Skip to content

Instantly share code, notes, and snippets.

@jredville
Created June 23, 2016 19:40
Show Gist options
  • Save jredville/87b8c67201469660ddd0334eff728ef7 to your computer and use it in GitHub Desktop.
Save jredville/87b8c67201469660ddd0334eff728ef7 to your computer and use it in GitHub Desktop.
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