Value receiver vs. pointer receiver: when to use what in Go?
Hello everyone! I've just started learning Go and I can't figure out receivers in methods. I think I understand the difference between value receiver and pointer receiver, but in practice I constantly doubt which one to choose.
For example, I have a struct:
```go
type User struct {
Name string
Age int
}
```
And I want to write a method that increments the user's age. Am I correct that a pointer receiver is needed here?
```go
func (u *User) Birthday() {
u.Age++
}
```
And if I just want to print information about the user, can I use a value receiver?
```go
func (u User) PrintInfo() {
fmt.Printf("Name: %s, Age: %d\n", u.Name, u.Age)
}
```
But here's what confuses me: I read somewhere that if the struct is large, then a value receiver
No answers yet.
Log in to leave an answer
Blogs