Skip to content

Instantly share code, notes, and snippets.

Created November 25, 2022 09:46
What would you like to do?
Go: Pointer receivers
package main
import "fmt"
type _struct struct {
field string
func (s *_struct) doStuff(msg string) {
s.field = msg
fmt.Println("I am doing stuff dammit, and field is ", s.field)
func (s _struct) doOtherStuff(msg string) {
s.field = msg
// Yeah, man, but this is just your copy.
fmt.Println("I am doing other stuff dammit, and field is ", s.field)
type _ifc interface {
doStuff(msg string)
doOtherStuff(msg string)
func main() {
var struct1 _struct
fmt.Println("Empty: ", struct1.field)
// See
// Automatically will call method with pointer receiver with &struct1:
// Still "left" because not a pointer receiver
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment