Last active
February 5, 2019 17:04
-
-
Save betandr/cdefec661c793859d797ce431b2b9e70 to your computer and use it in GitHub Desktop.
Go Named Reciever vs Pointer Receiver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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