Created
December 12, 2017 18:13
-
-
Save jonbodner/1bfe03c6086afc662974b93cd31766ad to your computer and use it in GitHub Desktop.
This file contains hidden or 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
type Foo struct { | |
A int | |
} | |
func (f Foo) Double() int { | |
return f.A * 2 | |
} | |
type Bar struct { | |
Foo | |
B int | |
} | |
type Doubler interface { | |
Double() int | |
} | |
func DoDouble(d Doubler) { | |
fmt.Println(d.Double()) | |
} | |
func main() { | |
f := Foo{10} | |
b := Bar{Foo: f, B: 20} | |
DoDouble(f) // passed in an instance of Foo; it meets the interface, so no surprise here | |
DoDouble(b) // passed in an instance of Bar; it works! | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment