I'm trying to learn Go. I learned that structs and arrays are copied by value (when passing them to functions or assigning them to variables). So we use pointers to allow modifying them and to save memory.
The thing is, in some situations I always find them use pointers to structs.
For instance, in an official web application tutorial they used the code
func (p *Page) save() error {
filename := p.Title + ".txt"
return ioutil.WriteFile(filename, p.Body, 0600)
}
Here it no data change occurs to the struct. This happens in may other places in official packages and 3rd party ones.
Another case is when they return a &struct{}. An example from the same link above:
func loadPage(title string) *Page {
filename := title + ".txt"
body, _ := ioutil.ReadFile(filename)
return &Page{Title: title, Body: body}
}
So, in which cases and places the pointer should be used?