Skip to content

Instantly share code, notes, and snippets.

@wegrata
Created September 30, 2013 17:50
Show Gist options
  • Save wegrata/6767486 to your computer and use it in GitHub Desktop.
Save wegrata/6767486 to your computer and use it in GitHub Desktop.
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