Skip to content

Instantly share code, notes, and snippets.

@betandr
Last active February 5, 2019 17:04
Show Gist options
  • Save betandr/cdefec661c793859d797ce431b2b9e70 to your computer and use it in GitHub Desktop.
Save betandr/cdefec661c793859d797ce431b2b9e70 to your computer and use it in GitHub Desktop.
Go Named Reciever vs Pointer Receiver
package main
import "fmt"
type Mutatable struct {
a int
b int
}
func (m Mutatable) UpdateReceiver() {
m.a = 5
m.b = 7
}
func (m *Mutatable) UpdatePointerReceiver() {
m.a = 5
m.b = 7
}
func main() {
m := &Mutatable{0, 0}
fmt.Printf("Original: %v\n", m)
m.UpdateReceiver()
fmt.Printf("Receiver: %v\n", m)
m.UpdatePointerReceiver()
fmt.Printf("Pointer Receiver: %v\n", m)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment