I'm trying to write a small program in which I have a few packages, each with a struct that implements an interface. The idea is that based on user input, I can choose what package to use to build a particular struct and then call a function on it that they're all supposed to have. Since I don't know the type ahead of time, I was under the impression that I could use a interface{}
and use that as a forward declaration (see the last code snippet). I have something that looks like this:
package foo
type FooInput struct {
Bar string
Baz int
}
type Foo interface {
Ding()
Dong()
}
In another package, bob
, I have something like:
type Bob struct {
foo.FooInput
}
func (mybob *Bob) Ding() {}
func (mybob *Bob) Dong() {}
func MakeBob(foo_input foo.FooInput) (*Bob, error) {
my_bob := Bob{foo_input}
return &my_bob, nil
}
In my main package, I have something that looks like so:
data = foo.FooInput("awyiss", 1}
var something interface{}
var err error
switch some_string {
case "foo":
something, err = bob.MakeBob(foo_input)
case "bar":
// imagine bar is like foo
something, err = bar.MakeBar(foo_input)
// imagine other cases
}
something.Dong()
When running / building / etc, I get the following error:
something.Dong undefined (type interface {} is interface with no methods)
I'm a bit confused as to what I'm doing wrong... any clarifiers on how I should use interface{}
(if at all) would be extremely helpful!