Skip to content

Instantly share code, notes, and snippets.

@jaydonnell
Last active December 17, 2015 01:19
Show Gist options
  • Save jaydonnell/5527772 to your computer and use it in GitHub Desktop.
Save jaydonnell/5527772 to your computer and use it in GitHub Desktop.
package main
import "fmt"
type Person struct {
Name string
}
func (p *Person) Intro() string {
return p.Name
}
func (p *Person) Hello() string {
return "hello from " + p.Name
}
type Woman struct {
Person
}
func (w *Woman) Intro() string {
return "Mrs. " + w.Person.Intro()
}
func main() {
w := new(Woman)
w.Name = "Diana"
fmt.Println(w.Intro())
fmt.Println(w.Hello())
fmt.Println(w.Person.Hello())
// this doesn't work
// w2 := &Woman{Name: "Lucy"}
// fmt.Println(w2.Hello())
// this works
p := &Person{Name: "Jay"} // <- struct literal
fmt.Println(p.Hello())
}
// ~/projects/goTest $ go run hello.go
// Mrs. Diana
// hello from Diana
// hello from Diana
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment