move: structs to root
This commit is contained in:
51
24-structs/01-intro/main.go
Normal file
51
24-structs/01-intro/main.go
Normal 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)
|
||||
}
|
63
24-structs/02-basics/main.go
Normal file
63
24-structs/02-basics/main.go
Normal 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)
|
||||
}
|
94
24-structs/03-compare-assign/main.go
Normal file
94
24-structs/03-compare-assign/main.go
Normal 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)
|
||||
}
|
||||
}
|
66
24-structs/04-embedding/main.go
Normal file
66
24-structs/04-embedding/main.go
Normal 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)
|
||||
}
|
19
24-structs/TODO.md
Normal file
19
24-structs/TODO.md
Normal file
@ -0,0 +1,19 @@
|
||||
[x] what?
|
||||
[x] why?
|
||||
[x] struct type
|
||||
[x] struct literal
|
||||
[x] anonymous structs
|
||||
[x] named structs
|
||||
[x] struct fields
|
||||
[x] compare and assign
|
||||
[x] printing
|
||||
[x] embedding
|
||||
[ ] exporting struct and fields
|
||||
[ ] struct tags {json encode/decode} - project?
|
||||
|
||||
**LATER:**
|
||||
[ ] funcs: constructor pattern
|
||||
[ ] pointers:
|
||||
[ ] structs and pointers - later
|
||||
[ ] padding and memory layout - later
|
||||
[ ] using anonymous structs when testing
|
21
24-structs/exercises/00-name/main.go
Normal file
21
24-structs/exercises/00-name/main.go
Normal 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() {
|
||||
|
||||
}
|
12
24-structs/exercises/00-name/solution/main.go
Normal file
12
24-structs/exercises/00-name/solution/main.go
Normal 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() {
|
||||
|
||||
}
|
5
24-structs/exercises/README.md
Normal file
5
24-structs/exercises/README.md
Normal file
@ -0,0 +1,5 @@
|
||||
# Structs Exercises
|
||||
|
||||
## Warm-Up
|
||||
|
||||
1. **[?](https://github.com/inancgumus/learngo/tree/master/???)**
|
31
24-structs/xxx-wizards/marshal/main.go
Normal file
31
24-structs/xxx-wizards/marshal/main.go
Normal file
@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Wizard is one of the greatest of people
|
||||
type Wizard struct {
|
||||
// name won't be marshalled (should be exported)
|
||||
Name string `json:name`
|
||||
Lastname string `json:"-"`
|
||||
Nick string `json:"nick"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
wizards := []Wizard{
|
||||
{Name: "Albert", Lastname: "Einstein", Nick: "emc2"},
|
||||
{Name: "Isaac", Lastname: "Newton", Nick: "apple"},
|
||||
{Name: "Stephen", Lastname: "Hawking", Nick: "blackhole"},
|
||||
{Name: "Marie", Lastname: "Curie", Nick: "radium"},
|
||||
{Name: "Charles", Lastname: "Darwin", Nick: "fittest"},
|
||||
}
|
||||
|
||||
bytes, err := json.Marshal(wizards)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Print(string(bytes))
|
||||
}
|
35
24-structs/xxx-wizards/unmarshal/main.go
Normal file
35
24-structs/xxx-wizards/unmarshal/main.go
Normal file
@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Wizard is one of the greatest of people
|
||||
type Wizard struct {
|
||||
// name won't be marshalled (should be exported)
|
||||
Name string `json:name`
|
||||
Lastname string `json:"-"`
|
||||
Nick string `json:"nick"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
file, err := ioutil.ReadFile("../marshal/wizards.json")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
wizards := make([]Wizard, 10)
|
||||
if json.Unmarshal(file, &wizards) != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Printf("%-15s %-15s\n%s",
|
||||
"Name", "Nick", strings.Repeat("=", 25))
|
||||
|
||||
for _, w := range wizards {
|
||||
fmt.Printf("%-15s %-15s\n", w.Name, w.Nick)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user