add: x-tba structs
This commit is contained in:
65
x-tba/structs/01-basics-part-i/main.go
Normal file
65
x-tba/structs/01-basics-part-i/main.go
Normal file
@ -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)
|
||||||
|
}
|
53
x-tba/structs/02-basics-part-ii/main.go
Normal file
53
x-tba/structs/02-basics-part-ii/main.go
Normal file
@ -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{}
|
||||||
|
}
|
20
x-tba/structs/TODO.md
Normal file
20
x-tba/structs/TODO.md
Normal file
@ -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)
|
19
x-tba/structs/xxx-using-in-maps/main.go
Normal file
19
x-tba/structs/xxx-using-in-maps/main.go
Normal file
@ -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])
|
||||||
|
}
|
47
x-tba/structs/xxx-wizards/cmd/marshal/main.go
Normal file
47
x-tba/structs/xxx-wizards/cmd/marshal/main.go
Normal file
@ -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))
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
}
|
1
x-tba/structs/xxx-wizards/cmd/marshal/wizards.json
Normal file
1
x-tba/structs/xxx-wizards/cmd/marshal/wizards.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
[{"Name":"Albert","nick":"emc2"},{"Name":"Isaac","nick":"apple"},{"Name":"Stephen","nick":"blackhole"},{"Name":"Marie","nick":"radium"},{"Name":"Charles","nick":"fittest"}]
|
29
x-tba/structs/xxx-wizards/cmd/unmarshal/main.go
Normal file
29
x-tba/structs/xxx-wizards/cmd/unmarshal/main.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
9
x-tba/structs/xxx-wizards/wizards.go
Normal file
9
x-tba/structs/xxx-wizards/wizards.go
Normal file
@ -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"`
|
||||||
|
}
|
8
x-tba/xxx-project-domain-visits.1/log.txt
Normal file
8
x-tba/xxx-project-domain-visits.1/log.txt
Normal file
@ -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
|
64
x-tba/xxx-project-domain-visits.1/main.go
Normal file
64
x-tba/xxx-project-domain-visits.1/main.go
Normal file
@ -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)
|
||||||
|
}
|
8
x-tba/xxx-project-domain-visits.2b/log.txt
Normal file
8
x-tba/xxx-project-domain-visits.2b/log.txt
Normal file
@ -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
|
82
x-tba/xxx-project-domain-visits.2b/main.go
Normal file
82
x-tba/xxx-project-domain-visits.2b/main.go
Normal file
@ -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])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user