Skip to content

Instantly share code, notes, and snippets.

@jonbodner
Created December 12, 2017 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonbodner/1bfe03c6086afc662974b93cd31766ad to your computer and use it in GitHub Desktop.
Save jonbodner/1bfe03c6086afc662974b93cd31766ad to your computer and use it in GitHub Desktop.
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