FAQ
I have a project I'm working on that will eventually be used to fetch data
from different sources for collation (hence the "Fetcher" interface). I'd
like this to be config file driven so that new data sources can be added on
the fly while reusing code, so I've been playing around with a factory type
architecture that will let me keep each type of fetcher in its own file,
while still registering its name so I can spin it up given only a string.

I can get it to work by making one small change (more on that in a moment),
but here's the code that doesn't work:

//factorytest.go
package main
import (
"fmt"
"work/factorytest/drivers"
)
func main() {
fmt.Println(drivers.GetMe("foo").Name())
fmt.Println(drivers.GetMe("bar").Name())
}
//drivers.go
package drivers
type Fetcher interface {
Name() string
}
var spawnerMap = make(map[string]func()Fetcher)
func GetMe(what string) (Fetcher) {
got := spawnerMap[what]
return got()
}
//foo.go
package drivers
type Foo struct {
name string
}
// To satisfy the interface
func (f Foo) Name() string {
return f.name
}
// To return a new item
func newFoo() (Foo) {
return Foo{name:"a new foo"}
}
// To register this item with the factory under this name
func init() {
spawnerMap["foo"] = newFoo
}

The error it gives me is:
drivers/foo.go:19: cannot use newFoo (type func() Foo) as type func()
Fetcher in assignment

If I change newFoo to
func newFoo() (Fetcher) {

It works just fine. My question is why do I need to do this? Shouldn't
the compiler know already that type Foo complies with the Fetcher interface
and know that something that returns a Foo is also returning a Fetcher?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • David Symonds at Jan 4, 2014 at 7:29 am
    spawnerMap is a map[string]func()Fetcher, which means its values must
    be of type func() Fetcher. It needs to be of that exact type, or an
    assignable type. Your newFoo is of type func() Foo, which is a
    different type, and not an assignable type.

    Your type Foo does satisfy the Fetcher interface, but func() Foo is
    not a func() Fetcher. Go does not have covariant types.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 4, '14 at 6:38a
activeJan 4, '14 at 7:29a
posts2
users2
websitegolang.org

2 users in discussion

David Symonds: 1 post Walkeraj: 1 post

People

Translate

site design / logo © 2021 Grokbase