go by example part 2

This commit is contained in:
2022-01-01 19:47:24 +01:00
parent afe165089d
commit c1b7c64da2
10 changed files with 335 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
package main
import "fmt"
// Gos structs are typed collections of fields. Theyre useful for grouping data together to form records.
// this person struct type has name and age fields
type person struct {
name string
age int
}
// newPerson constructs a new person struct with the given name
// safely return a pointer to local variable as a local variable will survive the scope of the function
func newPerson(name string) *person {
p := person{name: name}
p.age = 42
return &p
}
func main() {
// create a new struct
fmt.Println(person{"Bob", 20})
// name the fields when initializing a struct
fmt.Println(person{name: "Alice", age: 30})
// omitted fields will be zero-valued
fmt.Println(person{name: "Fred"})
// & prefix yields a pointer to the struct
fmt.Println(&person{name: "Ann", age: 40})
// encapsulate new struct creation in constructor functions (ideomatic)
fmt.Println(newPerson("Jon"))
// access struct fields with a dot
s := person{name: "Sean", age: 50}
fmt.Println(s.name)
// use dots with struct pointers
// the pointers are automatically dereferenced
sp := &s
fmt.Println(sp.age)
// structs are mutable
sp.age = 51
fmt.Println(sp.age)
}