Created
March 2, 2014 07:22
-
-
Save rolaveric/9303148 to your computer and use it in GitHub Desktop.
Example of using an empty interface to work around the type system in Go
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
// X needs anything | |
func X(anything interface{}) { | |
// Nothing comes for free - need to see what I got | |
switch value := anything.(type) { | |
case string: | |
fmt.Printf("I got a string!: %s", value) | |
default: | |
fmt.Print("I got... something!") | |
} | |
} | |
func main() { | |
X("meep") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment