Last active
December 17, 2015 01:19
-
-
Save jaydonnell/5527772 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 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