Skip to content

Instantly share code, notes, and snippets.

@EnchanterIO
Last active August 21, 2018 18:43
Show Gist options
  • Save EnchanterIO/4ad280eab2089c61c1fcccdbb979bb81 to your computer and use it in GitHub Desktop.
Save EnchanterIO/4ad280eab2089c61c1fcccdbb979bb81 to your computer and use it in GitHub Desktop.
golang_mastery_struct_vs_object
// Copyright 2018 Lukas Lukac <https://lukaslukac.io>; All rights reserved.
// Play: https://play.golang.org/p/FQa09CzdtRh
package main
import (
"fmt"
)
type illusion struct {
magicianCount int
}
func (i illusion) increaseMagicianCount() {
i.magicianCount++
}
func main() {
myIllusion := illusion{}
myIllusion.increaseMagicianCount()
fmt.Println(myIllusion.magicianCount) // Expected: 1, Surprisingly: 0
// ↟ equivalent to ↡
myIllusion = illusion{}
increaseMagicianCount(myIllusion)
fmt.Println(myIllusion.magicianCount) // Expected: 0, Not surprisingly: 0
}
func increaseMagicianCount(i illusion) {
i.magicianCount++
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment