Skip to content

Instantly share code, notes, and snippets.

@fgm
Last active August 29, 2015 14:06
Show Gist options
  • Save fgm/64821c0cee261bac3566 to your computer and use it in GitHub Desktop.
Save fgm/64821c0cee261bac3566 to your computer and use it in GitHub Desktop.
// Inspired by http://talks.golang.org/2014/go4java.slide#23
package main
import "github.com/davecgh/go-spew/spew"
type Person struct {
name string
}
func (p *Person) Name() string {
if p == nil {
return "Anonymous"
} else {
return p.name
}
}
func NewPerson(name string) *Person {
var p = new(Person)
p.name = name
return p
}
func main() {
var p1, p2 *Person
p1 = NewPerson("Nemo")
spew.Dump(p1.Name(), p2, p2.Name())
}
/* Result
(string) (len=4) "Nemo"
(*main.Person)(<nil>)
(string) (len=9) "Anonymous"
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment