Last active
August 29, 2015 14:06
-
-
Save fgm/64821c0cee261bac3566 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
// 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