I've defined an interface type, and a concrete type that I think implements that interface, but when I use it in a list, I get an error from the compiler that makes it look like it doesn't.
Here's my interface type, in anything/anything.go:
package anything
import (
"fmt"
)
type AnyThing interface {
Textual() string
Numeric(name string) int
}
func OneCallee(thing AnyThing) {
fmt.Printf("textual result is %s and numeric result is %d\n",
thing.Textual(), thing.Numeric("default"))
}
func ListCallee(things []AnyThing) {
fmt.Printf("textual result is %s and numeric result is %d\n",
things[0].Textual(), things[0].Numeric("default"))
}
and here's a concrete type that defines the methods required by the
interface, in particularthing/particularthing.go:
package particularthing
type ParticularThing struct {
Name string
Number int
}
func (n ParticularThing) Textual() string {
return n.Name
}
func (n ParticularThing) Numeric(name string) int {
return n.Number
}
and here's the caller:
package main
import (
"example.com/user/scratch/anything"
"example.com/user/scratch/particularthing"
)
func main() {
onething := particularthing.ParticularThing{"this", 1}
anything.OneCallee(onething)
thinglist := make([]particularthing.ParticularThing, 1)
anything.ListCallee(thinglist)
}
When I compile it, I get:
./caller.go:12:21: cannot use thinglist (type
[]particularthing.ParticularThing) as type []anything.AnyThing in argument
to anything.ListCallee
Could some explain what the problem with this is?
I've attached a tarball of the files to make experimental changes easier.
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/3abb649b-f134-4535-8832-18c8ca0a54ebo%40googlegroups.com.
minimal.tar
Description: Unix tar archive
