From 4f60828fffa0479a626333cac89ad36a3abd66d0 Mon Sep 17 00:00:00 2001 From: Inanc Gumus Date: Wed, 17 Apr 2019 23:17:45 +0300 Subject: [PATCH] add: more content to structs --- x-tba/2-structs/01-basics-part-i/main.go | 65 ------------- x-tba/2-structs/01-intro/main.go | 51 ++++++++++ x-tba/2-structs/02-basics-part-ii/main.go | 53 ----------- x-tba/2-structs/02-basics/main.go | 63 +++++++++++++ x-tba/2-structs/03-compare-assign/main.go | 94 +++++++++++++++++++ x-tba/2-structs/04-embedding/main.go | 66 +++++++++++++ x-tba/2-structs/exercises/00-name/main.go | 21 +++++ .../exercises/00-name/solution/main.go | 12 +++ x-tba/2-structs/exercises/README.md | 5 + 9 files changed, 312 insertions(+), 118 deletions(-) delete mode 100644 x-tba/2-structs/01-basics-part-i/main.go create mode 100644 x-tba/2-structs/01-intro/main.go delete mode 100644 x-tba/2-structs/02-basics-part-ii/main.go create mode 100644 x-tba/2-structs/02-basics/main.go create mode 100644 x-tba/2-structs/03-compare-assign/main.go create mode 100644 x-tba/2-structs/04-embedding/main.go create mode 100644 x-tba/2-structs/exercises/00-name/main.go create mode 100644 x-tba/2-structs/exercises/00-name/solution/main.go create mode 100644 x-tba/2-structs/exercises/README.md diff --git a/x-tba/2-structs/01-basics-part-i/main.go b/x-tba/2-structs/01-basics-part-i/main.go deleted file mode 100644 index cd6dc48..0000000 --- a/x-tba/2-structs/01-basics-part-i/main.go +++ /dev/null @@ -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) -} diff --git a/x-tba/2-structs/01-intro/main.go b/x-tba/2-structs/01-intro/main.go new file mode 100644 index 0000000..4e91ada --- /dev/null +++ b/x-tba/2-structs/01-intro/main.go @@ -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) +} diff --git a/x-tba/2-structs/02-basics-part-ii/main.go b/x-tba/2-structs/02-basics-part-ii/main.go deleted file mode 100644 index ddb7b9f..0000000 --- a/x-tba/2-structs/02-basics-part-ii/main.go +++ /dev/null @@ -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{} -} diff --git a/x-tba/2-structs/02-basics/main.go b/x-tba/2-structs/02-basics/main.go new file mode 100644 index 0000000..8cb45d2 --- /dev/null +++ b/x-tba/2-structs/02-basics/main.go @@ -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) +} diff --git a/x-tba/2-structs/03-compare-assign/main.go b/x-tba/2-structs/03-compare-assign/main.go new file mode 100644 index 0000000..31d161c --- /dev/null +++ b/x-tba/2-structs/03-compare-assign/main.go @@ -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) + } +} diff --git a/x-tba/2-structs/04-embedding/main.go b/x-tba/2-structs/04-embedding/main.go new file mode 100644 index 0000000..0a953b5 --- /dev/null +++ b/x-tba/2-structs/04-embedding/main.go @@ -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) +} diff --git a/x-tba/2-structs/exercises/00-name/main.go b/x-tba/2-structs/exercises/00-name/main.go new file mode 100644 index 0000000..50f6bc1 --- /dev/null +++ b/x-tba/2-structs/exercises/00-name/main.go @@ -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() { + +} diff --git a/x-tba/2-structs/exercises/00-name/solution/main.go b/x-tba/2-structs/exercises/00-name/solution/main.go new file mode 100644 index 0000000..bd7e329 --- /dev/null +++ b/x-tba/2-structs/exercises/00-name/solution/main.go @@ -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() { + +} diff --git a/x-tba/2-structs/exercises/README.md b/x-tba/2-structs/exercises/README.md new file mode 100644 index 0000000..0982f4e --- /dev/null +++ b/x-tba/2-structs/exercises/README.md @@ -0,0 +1,5 @@ +# Structs Exercises + +## Warm-Up + +1. **[?](https://github.com/inancgumus/learngo/tree/master/???)** \ No newline at end of file