Skip to content

Instantly share code, notes, and snippets.

@betandr
Last active February 5, 2019 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save betandr/fced026d8ac63184e7098eb9b0909c5b to your computer and use it in GitHub Desktop.
Save betandr/fced026d8ac63184e7098eb9b0909c5b to your computer and use it in GitHub Desktop.
Pointer vs Named Receivers
package main
import "fmt"
type SomeThing struct {
Msg string
}
func (t SomeThing) SomeNamedReciever(msg string) {
t.Msg = msg
fmt.Println(t.Msg) // ¡Hola Mundo!
}
func (t *SomeThing) SomePointerReciever(msg string) {
t.Msg = msg
fmt.Println(t.Msg) // こんにちは世界
}
func main() {
t := new(SomeThing)
t.Msg = "Hello, World!"
fmt.Println(t.Msg) // Hello, World!
t.SomeNamedReciever("¡Hola Mundo!")
fmt.Println(t.Msg) // Hello, World!
t.SomePointerReciever("こんにちは世界")
fmt.Println(t.Msg) // こんにちは世界
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment