Created
June 23, 2016 19:40
-
-
Save jredville/87b8c67201469660ddd0334eff728ef7 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 ( | |
"log" | |
) | |
type Foo interface { | |
Bar() string | |
} | |
type FooFactory func() Foo | |
type A struct { | |
bar string | |
} | |
func NewA() *A { | |
return &A{bar: "hello"} | |
} | |
func (a *A) Bar() string { | |
return a.bar | |
} | |
type B struct { | |
baz string | |
} | |
func NewB() *B { | |
return &B{baz: "world"} | |
} | |
func (b *B) Bar() string { | |
return b.baz | |
} | |
func Printer(fac FooFactory) { | |
log.Println(fac().Bar()) | |
} | |
func main() { | |
Printer(NewA) //cannot use NewA (type func() *A) as type FooFactory in argument to Printer | |
Printer(NewB) //cannot use NewB (type func() *B) as type FooFactory in argument to Printer | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment