add: more content to structs

This commit is contained in:
Inanc Gumus
2019-04-17 23:17:45 +03:00
parent 3644c4f2cd
commit 4f60828fff
9 changed files with 312 additions and 118 deletions

View File

@ -1,65 +0,0 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import "fmt"
func main() {
/*
USING VARIABLES
*/
// var (
// name, lastname string
// age int
// )
//
// name, lastname = "Pablo", "Picasso"
// age = 95
//
// name2, lastname2 := "Sigmund", "Freud"
// age2 := 83
//
// fmt.Println("Picasso:", name, lastname, age)
// fmt.Println("Freud :", name2, lastname2, age2)
/*
USING STRUCT VARIABLES
*/
// // declare a struct variable
// var picasso struct {
// name, lastname string
// age int
// }
// create a new struct type
type person struct {
name, lastname string
age int
}
// short-declare struct variable without field names
// you cannot omit values when you initialize without field names
picasso := person{
name: "Pablo",
lastname: "Picasso",
age: 95,
}
// short-declare struct variable with field names
// omitted fields will be zeroed
freud := person{
name: "Sigmund",
lastname: "Freud",
age: 83,
}
fmt.Printf("Picasso: %#v\n\n", picasso)
fmt.Printf("Freud : %#v\n\n", freud)
}

View File

@ -0,0 +1,51 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import "fmt"
func main() {
type Movie struct {
Title string
Genre string
Rating int
}
type Rental struct {
Address string
Rooms int
Size int
Price int
}
type Person struct {
Name string
Lastname string
Age int
}
person1 := Person{Name: "Pablo", Lastname: "Picasso", Age: 91}
person2 := Person{Name: "Sigmund", Lastname: "Freud", Age: 83}
fmt.Printf("person1: %+v\n", person1)
fmt.Printf("person2: %+v\n", person2)
type VideoGame struct {
Title string
Genre string
Published bool
}
pacman := VideoGame{
Title: "Pac-Man",
Genre: "Arcade Game",
Published: true,
}
fmt.Printf("pacman: %+v\n", pacman)
}

View File

@ -1,53 +0,0 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import "fmt"
func main() {
// create a new struct type
type person struct {
name, lastname string
age int
}
picasso := person{"Pablo", "Picasso", 95}
freud := person{name: "Sigmund", lastname: "Freud", age: 83}
// picasso.age++
// picasso.name = "Master"
master := picasso
// var master person
// master.name = picasso.name
// master.lastname = picasso.lastname
// master.age = picasso.age
master = freud
master.name = "Master"
master.age++
for _, p := range []person{picasso, freud, master} {
if p == picasso {
// if p.name == picasso.name &&
// p.lastname == picasso.lastname &&
// p.age == picasso.age {
// p is a copy
p.name = "Raphael"
fmt.Printf("%s's age is %d\n", p.name, p.age)
}
}
// Picasso is still the same
fmt.Printf("\n%s's age is %d\n", picasso.name, picasso.age)
// type team struct{ scores []int }
// _ = team{} == team{}
}

View File

@ -0,0 +1,63 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import "fmt"
func main() {
/*
USING VARIABLES
*/
var (
name, lastname string
age int
name2, lastname2 string
age2 int
)
name, lastname, age = "Pablo", "Picasso", 95
name2, lastname2, age = "Sigmund", "Freud", 83
fmt.Println("Picasso:", name, lastname, age)
fmt.Println("Freud :", name2, lastname2, age2)
// var picasso struct {
// name, lastname string
// age int
// }
// var freud struct {
// name, lastname string
// age int
// }
// create a new struct type
type person struct {
name, lastname string
age int
}
// picasso := person{name: "Pablo", lastname: "Picasso", age: 91}
picasso := person{
name: "Pablo",
lastname: "Picasso",
age: 91,
}
var freud person
freud.name = "Sigmund"
freud.lastname = "Freud"
freud.age = 83
fmt.Printf("\n%s's age is %d\n", picasso.lastname, picasso.age)
fmt.Printf("\nPicasso: %#v\n", picasso)
fmt.Printf("Freud : %#v\n", freud)
}

View File

@ -0,0 +1,94 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import "fmt"
// #1b: create the song struct type
type song struct {
title, artist string
}
// #5: structs can contain other structs
type playlist struct {
genre string
// songTitles []string
// songArtist []string
// #6: include a slice of song structs
songs []song
}
func main() {
// #1: create two struct values with the same type
song1 := song{title: "wonderwall", artist: "oasis"}
song2 := song{title: "super sonic", artist: "oasis"}
fmt.Printf("song1: %+v\nsong2: %+v\n", song1, song2)
// #4: structs are copied
// song1 = song2
// #3: structs can be compared
if song1 == song2 {
// #2: struct comparison works like this
// if song1.title == song2.title &&
// song1.artist == song2.artist {
fmt.Println("songs are equal.")
} else {
fmt.Println("songs are not equal.")
}
// #8
songs := []song{
// #7b: you don't have to type the element types
{title: "wonderwall", artist: "oasis"},
{title: "radioactive", artist: "imagine dragons"},
}
// #7: a struct can include another struct
rock := playlist{
genre: "indie rock",
songs: songs,
}
// #9: you can't compare struct values that contains incomparable fields
// you need to compare them manually
// clone := rock
// if rock.songs == clone {
// }
// if songs == songs {
// #11: song is a clone, it cannot change the original struct value
song := rock.songs[0]
song.title = "live forever"
// #11c: directly set the original one
rock.songs[0].title = "live forever"
// #11b
fmt.Printf("\n%+v\n%+v\n", song, rock.songs[0])
// #10: printing
fmt.Printf("\n%-20s %20s\n", "TITLE", "ARTIST")
for _, s := range rock.songs {
// s := rock.songs[i]
// #12b: s is a copy inside because struct values are copied
s.title = "destroy"
fmt.Printf("%-20s %20s\n", s.title, s.artist)
}
// #12
fmt.Printf("\n%-20s %20s\n", "TITLE", "ARTIST")
for _, s := range rock.songs {
fmt.Printf("%-20s %20s\n", s.title, s.artist)
}
}

View File

@ -0,0 +1,66 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
)
func main() {
// #1: declare the types
type text struct {
title string
words int
}
type book struct {
// title string
// words int
// #3: include the text as a field
// text text
// #4: embed the text
text
isbn string
// #5: add a conflicting field
title string
}
// #2: print a book
// moby := book{title: "moby dick", words: 206052, isbn: "102030"}
// fmt.Printf("%s has %d words (isbn: %s)\n", moby.title, moby.words, moby.isbn)
// #3b: type the text in its own field
moby := book{
// #5c: type the field in a new field
// title: "conflict",
text: text{title: "moby dick", words: 206052},
isbn: "102030",
}
moby.text.words = 1000
moby.words++
// // #4b: print the book
fmt.Printf("%s has %d words (isbn: %s)\n",
moby.title, // equals to: moby.text.title
moby.words, // equals to: moby.text.words
moby.isbn)
// #3c: print the book
// fmt.Printf("%s has %d words (isbn: %s)\n",
// moby.text.title, moby.text.words, moby.isbn)
// #5b: print the conflict
fmt.Printf("%#v\n", moby)
// go get -u github.com/davecgh/go-spew/spew
// spew.Dump(moby)
}

View File

@ -0,0 +1,21 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
// ---------------------------------------------------------
// EXERCISE: ??
//
//
// EXPECTED OUTPUT
//
//
// ---------------------------------------------------------
func main() {
}

View File

@ -0,0 +1,12 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
func main() {
}

View File

@ -0,0 +1,5 @@
# Structs Exercises
## Warm-Up
1. **[?](https://github.com/inancgumus/learngo/tree/master/???)**