go_playground/5-go-by-example/18-structs.go

50 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)
}