move: structs to root
This commit is contained in:
@@ -1,51 +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() {
|
||||
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)
|
||||
}
|
@@ -1,63 +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
|
||||
|
||||
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)
|
||||
}
|
@@ -1,94 +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"
|
||||
|
||||
// #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)
|
||||
}
|
||||
}
|
@@ -1,66 +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() {
|
||||
// #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)
|
||||
}
|
@@ -1 +0,0 @@
|
||||
This section is in progress. I'm working hard to update the course all the time. Hold on!
|
@@ -1,20 +0,0 @@
|
||||
[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
|
||||
[ ] embedding
|
||||
|
||||
**LATER:**
|
||||
[ ] exporting struct and fields
|
||||
[ ] funcs: constructor pattern
|
||||
[ ] using anonymous structs when testing
|
||||
[ ] pointers:
|
||||
[ ] struct tags {json encode/decode} - project?
|
||||
[ ] structs and pointers - later
|
||||
[ ] padding and memory layout - later
|
||||
[ ] empty struct (in channels section)
|
@@ -1,21 +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
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE: ??
|
||||
//
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
//
|
||||
//
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
|
||||
}
|
@@ -1,12 +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
|
||||
|
||||
func main() {
|
||||
|
||||
}
|
@@ -1,5 +0,0 @@
|
||||
# Structs Exercises
|
||||
|
||||
## Warm-Up
|
||||
|
||||
1. **[?](https://github.com/inancgumus/learngo/tree/master/???)**
|
@@ -1,19 +0,0 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
type book struct {
|
||||
name, isbn string
|
||||
}
|
||||
|
||||
kafka := book{"Kafka's Revenge", "S-001"}
|
||||
golden := book{"Stay Golden", "S-002"}
|
||||
|
||||
books := make(map[book]int, 2)
|
||||
books[kafka] = 100
|
||||
books[golden] = 50
|
||||
|
||||
fmt.Printf("%s sold %d times\n", kafka.name, books[kafka])
|
||||
fmt.Printf("%s sold %d times\n", golden.name, books[golden])
|
||||
}
|
@@ -1,47 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/inancgumus/learngo/x-tba/structs/xxx-json/wizards"
|
||||
)
|
||||
|
||||
func main() {
|
||||
wizards := []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)
|
||||
}
|
||||
|
||||
ioutil.WriteFile("wizards.json", bytes, 0644)
|
||||
|
||||
//
|
||||
// PREVIOUSLY
|
||||
//
|
||||
|
||||
// names := [...][3]string{
|
||||
// {"First Name", "Last Name", "Nickname"},
|
||||
// {"Albert", "Einstein", "emc2"},
|
||||
// {"Isaac", "Newton", "apple"},
|
||||
// {"Stephen", "Hawking", "blackhole"},
|
||||
// {"Marie", "Curie", "radium"},
|
||||
// {"Charles", "Darwin", "fittest"},
|
||||
// }
|
||||
|
||||
// for i := range names {
|
||||
// n := names[i]
|
||||
// fmt.Printf("%-15s %-15s %-15s\n", n[0], n[1], n[2])
|
||||
|
||||
// if i == 0 {
|
||||
// fmt.Println(strings.Repeat("=", 50))
|
||||
// }
|
||||
// }
|
||||
}
|
@@ -1 +0,0 @@
|
||||
[{"Name":"Albert","nick":"emc2"},{"Name":"Isaac","nick":"apple"},{"Name":"Stephen","nick":"blackhole"},{"Name":"Marie","nick":"radium"},{"Name":"Charles","nick":"fittest"}]
|
@@ -1,29 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
|
||||
"github.com/inancgumus/learngo/x-tba/structs/xxx-json/wizards"
|
||||
)
|
||||
|
||||
func main() {
|
||||
file, err := ioutil.ReadFile("../marshal/wizards.json")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
wizards := make([]wizards.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)
|
||||
}
|
||||
}
|
@@ -1,9 +0,0 @@
|
||||
package wizards
|
||||
|
||||
// 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"`
|
||||
}
|
Reference in New Issue
Block a user