From 2cd646b0922b9e1a29eb77882afaec546713688b Mon Sep 17 00:00:00 2001 From: Inanc Gumus Date: Wed, 10 Apr 2019 21:00:05 +0300 Subject: [PATCH] add: x-tba structs --- x-tba/structs/01-basics-part-i/main.go | 65 +++++++++++++++ x-tba/structs/02-basics-part-ii/main.go | 53 ++++++++++++ x-tba/structs/TODO.md | 20 +++++ x-tba/structs/xxx-using-in-maps/main.go | 19 +++++ x-tba/structs/xxx-wizards/cmd/marshal/main.go | 47 +++++++++++ .../xxx-wizards/cmd/marshal/wizards.json | 1 + .../structs/xxx-wizards/cmd/unmarshal/main.go | 29 +++++++ x-tba/structs/xxx-wizards/wizards.go | 9 ++ x-tba/xxx-project-domain-visits.1/log.txt | 8 ++ x-tba/xxx-project-domain-visits.1/main.go | 64 +++++++++++++++ x-tba/xxx-project-domain-visits.2b/log.txt | 8 ++ x-tba/xxx-project-domain-visits.2b/main.go | 82 +++++++++++++++++++ 12 files changed, 405 insertions(+) create mode 100644 x-tba/structs/01-basics-part-i/main.go create mode 100644 x-tba/structs/02-basics-part-ii/main.go create mode 100644 x-tba/structs/TODO.md create mode 100644 x-tba/structs/xxx-using-in-maps/main.go create mode 100644 x-tba/structs/xxx-wizards/cmd/marshal/main.go create mode 100644 x-tba/structs/xxx-wizards/cmd/marshal/wizards.json create mode 100644 x-tba/structs/xxx-wizards/cmd/unmarshal/main.go create mode 100644 x-tba/structs/xxx-wizards/wizards.go create mode 100644 x-tba/xxx-project-domain-visits.1/log.txt create mode 100644 x-tba/xxx-project-domain-visits.1/main.go create mode 100644 x-tba/xxx-project-domain-visits.2b/log.txt create mode 100644 x-tba/xxx-project-domain-visits.2b/main.go diff --git a/x-tba/structs/01-basics-part-i/main.go b/x-tba/structs/01-basics-part-i/main.go new file mode 100644 index 0000000..cd6dc48 --- /dev/null +++ b/x-tba/structs/01-basics-part-i/main.go @@ -0,0 +1,65 @@ +// 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/structs/02-basics-part-ii/main.go b/x-tba/structs/02-basics-part-ii/main.go new file mode 100644 index 0000000..ddb7b9f --- /dev/null +++ b/x-tba/structs/02-basics-part-ii/main.go @@ -0,0 +1,53 @@ +// 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/structs/TODO.md b/x-tba/structs/TODO.md new file mode 100644 index 0000000..2d48e15 --- /dev/null +++ b/x-tba/structs/TODO.md @@ -0,0 +1,20 @@ +[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) \ No newline at end of file diff --git a/x-tba/structs/xxx-using-in-maps/main.go b/x-tba/structs/xxx-using-in-maps/main.go new file mode 100644 index 0000000..f39d174 --- /dev/null +++ b/x-tba/structs/xxx-using-in-maps/main.go @@ -0,0 +1,19 @@ +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]) +} diff --git a/x-tba/structs/xxx-wizards/cmd/marshal/main.go b/x-tba/structs/xxx-wizards/cmd/marshal/main.go new file mode 100644 index 0000000..4fe6503 --- /dev/null +++ b/x-tba/structs/xxx-wizards/cmd/marshal/main.go @@ -0,0 +1,47 @@ +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)) + // } + // } +} diff --git a/x-tba/structs/xxx-wizards/cmd/marshal/wizards.json b/x-tba/structs/xxx-wizards/cmd/marshal/wizards.json new file mode 100644 index 0000000..e73e71e --- /dev/null +++ b/x-tba/structs/xxx-wizards/cmd/marshal/wizards.json @@ -0,0 +1 @@ +[{"Name":"Albert","nick":"emc2"},{"Name":"Isaac","nick":"apple"},{"Name":"Stephen","nick":"blackhole"},{"Name":"Marie","nick":"radium"},{"Name":"Charles","nick":"fittest"}] \ No newline at end of file diff --git a/x-tba/structs/xxx-wizards/cmd/unmarshal/main.go b/x-tba/structs/xxx-wizards/cmd/unmarshal/main.go new file mode 100644 index 0000000..7c33ba8 --- /dev/null +++ b/x-tba/structs/xxx-wizards/cmd/unmarshal/main.go @@ -0,0 +1,29 @@ +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) + } +} diff --git a/x-tba/structs/xxx-wizards/wizards.go b/x-tba/structs/xxx-wizards/wizards.go new file mode 100644 index 0000000..121cc85 --- /dev/null +++ b/x-tba/structs/xxx-wizards/wizards.go @@ -0,0 +1,9 @@ +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"` +} diff --git a/x-tba/xxx-project-domain-visits.1/log.txt b/x-tba/xxx-project-domain-visits.1/log.txt new file mode 100644 index 0000000..34e1d38 --- /dev/null +++ b/x-tba/xxx-project-domain-visits.1/log.txt @@ -0,0 +1,8 @@ +learngoprogramming.com 10 +learngoprogramming.com 5 +learngoprogramming.com 10 +learngoprogramming.com 20 +golang.org 4 +golang.org 6 +golang.org 5 +golang.org 10 diff --git a/x-tba/xxx-project-domain-visits.1/main.go b/x-tba/xxx-project-domain-visits.1/main.go new file mode 100644 index 0000000..a4c1a8f --- /dev/null +++ b/x-tba/xxx-project-domain-visits.1/main.go @@ -0,0 +1,64 @@ +// 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 ( + "bufio" + "fmt" + "os" + "strconv" + "strings" +) + +func main() { + var ( + // use a comparable struct for the keys + visits = make(map[string]int) + + domains []string + dupDomains = make(map[string]bool) + + line int + ) + + in := bufio.NewScanner(os.Stdin) + for in.Scan() { + line++ + + fields := strings.Fields(in.Text()) + if len(fields) != 2 { + fmt.Printf("Wrong input: %v\n", fields) + return + } + + domain, svisit := fields[0], fields[1] + + dvisit, err := strconv.Atoi(svisit) + if err != nil { + fmt.Printf("Wrong input: %q (line #%d)\n", svisit, line) + return + } + visits[domain] += dvisit + + // only add the domain once + if !dupDomains[domain] { + domains = append(domains, domain) + dupDomains[domain] = true + } + } + + // print the summary grouped by domain + var total int + for _, d := range domains { + v := visits[d] + fmt.Printf("%-25s -> %d\n", d, v) + total += v + } + + fmt.Printf("\n%-25s -> %d\n", "TOTAL", total) +} diff --git a/x-tba/xxx-project-domain-visits.2b/log.txt b/x-tba/xxx-project-domain-visits.2b/log.txt new file mode 100644 index 0000000..1cbc9f9 --- /dev/null +++ b/x-tba/xxx-project-domain-visits.2b/log.txt @@ -0,0 +1,8 @@ +learngoprogramming.com turkey 10 +learngoprogramming.com turkey 5 +learngoprogramming.com usa 10 +learngoprogramming.com usa 20fds +golang.org germany 4 +golang.org germany 6 +golang.org italy 5 +golang.org italy 10 diff --git a/x-tba/xxx-project-domain-visits.2b/main.go b/x-tba/xxx-project-domain-visits.2b/main.go new file mode 100644 index 0000000..3260cc7 --- /dev/null +++ b/x-tba/xxx-project-domain-visits.2b/main.go @@ -0,0 +1,82 @@ +// 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 ( + "bufio" + "fmt" + "os" + "strconv" + "strings" +) + +func main() { + type visit struct { + domain string + country string + } + + var ( + // use a comparable struct for the keys + visits = make(map[visit]int) + + domains, countries []string + dupDomains = make(map[string]bool) + dupCountries = make(map[string]bool) + + line int + ) + + in := bufio.NewScanner(os.Stdin) + for in.Scan() { + line++ + + fields := strings.Fields(in.Text()) + if len(fields) != 3 { + fmt.Printf("Wrong input: %v\n", fields) + return + } + + v := visit{ + domain: fields[0], + country: fields[1], + } + + // increase the visit counts per domain + country + fhits := fields[2] + + hits, err := strconv.Atoi(fhits) + if err != nil { + fmt.Printf("Wrong input: %q (line #%d)\n", fhits, line) + return + } + visits[v] += hits + + // only add the domain once + if !dupDomains[v.domain] { + domains = append(domains, v.domain) + dupDomains[v.domain] = true + } + + // only add the country once + if !dupCountries[v.country] { + countries = append(countries, v.country) + dupCountries[v.country] = true + } + } + + // print the summary grouped by domain + for _, d := range domains { + fmt.Println(d) + + for _, c := range countries { + v := visit{domain: d, country: c} + fmt.Printf("\t%s %d\n", c, visits[v]) + } + } +}