Last active
August 21, 2018 18:43
-
-
Save EnchanterIO/4ad280eab2089c61c1fcccdbb979bb81 to your computer and use it in GitHub Desktop.
golang_mastery_struct_vs_object
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
// 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