Created
September 30, 2013 17:50
-
-
Save wegrata/6767486 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"fmt" | |
) | |
type IntWrapper struct { | |
x int | |
} | |
func (this IntWrapper) value() int { | |
return this.x | |
} | |
type Valler interface { | |
value() int | |
} | |
//this will except a value, pointer, or nil without being specified in the function signature | |
func do_stuff_interface(x Valler) { | |
fmt.Println(x.value()) | |
} | |
//won't compile if you pass a pointer/nil | |
func do_stuff_safe(x IntWrapper) { | |
fmt.Println(x.value()) | |
} | |
//accepts nil, but at least x is marked as a pointer in the function signature | |
func do_stuff_unsafe(x *IntWrapper) { | |
fmt.Println(x.value()) | |
} | |
func main() { | |
x := IntWrapper{x: 5} | |
do_stuff_interface(&x) | |
do_stuff_interface(x) | |
do_stuff_unsafe(&x) | |
do_stuff_safe(x) | |
do_stuff_unsafe(nil) | |
do_stuff_interface(nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment