A struct is a user-defined type that contains named fields.
// Anonymous struct literal (declaration + initialization).
p := struct {
Name string
Age int
}{
Name: "John",
Age: 36,
}
Accessing struct pointers
If you make a pointer to a struct, you can access its fields in two ways:
// Named struct (declaration).
type Person struct {
Name string
Age int
}
func main() {
p := &Person{"Joe", 42}
fmt.Println((*p).Name) // explicit dereferencing; it's cumbersome
fmt.Println(p.Name) // implicit dereferencing done by the language
}
Populating structs
Rather than populating a struct by passing a pointer to it into a function, have the function instantiate and return the struct.
Don’t do this:
func MakePerson(p *Person) error {
p.Name = "John"
p.Age = 36
return nil
}
Do this:
func MakePerson() (*Person, error) {
p := Person{
Name: "John",
Age: 36,
}
return &p, nil
}
One exception is when a function expects an interface:
p := struct {
Name string
Age int
}{}
err := json.Unmarshal([]byte(`{"Name": "John", "Age": 36}`), &p)