-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Go | Automatic (implicit) pointer dereferencing for pointer receivers #85
Comments
From https://www.digitalocean.com/community/tutorials/defining-methods-in-go: package main
import "fmt"
type Boat struct {
Name string
occupants []string
}
func (b *Boat) AddOccupant(name string) *Boat {
b.occupants = append(b.occupants, name)
return b
}
func (b Boat) Manifest() {
fmt.Println("The", b.Name, "has the following occupants:")
for _, n := range b.occupants {
fmt.Println("\t", n)
}
}
func main() {
b := &Boat{
Name: "S.S. DigitalOcean",
}
b.AddOccupant("Sammy the Shark")
b.AddOccupant("Larry the Lobster")
b.Manifest()
} Snippet from description:
Worth stressing:
|
From
A little further down:
and further:
I obviously need to spend more time with this book (very detailed). |
From Get Programming with Go:
|
This is the example code from the https://www.udemy.com/course/go-the-complete-developers-guide/ course:
Here it looks like they're explicitly dereferencing the pointer receiver in order to get at the
firstName
field. I vaguely recall reading about Go automatically dereferencing for you with pointer receivers, so for the programmer's perspective this would be functionally equivalent (though less explicit perhaps?):Need to do some additional research to confirm that my memory is correct and if possible, figure out what the best practice is for this.
The text was updated successfully, but these errors were encountered: