它实现的接口片?

我有一个接口 Model,它是由 structPerson实现的。

要获得一个模型实例,我有以下 helper 函数:

func newModel(c string) Model {
switch c {
case "person":
return newPerson()
}
return nil
}


func newPerson() *Person {
return &Person{}
}

上面的方法允许我返回一个正确类型的 Person 实例(以后可以用同样的方法轻松地添加新模型)。

当我试图为返回一个模型片段做类似的事情时,我得到了一个错误。代码:

func newModels(c string) []Model {
switch c {
case "person":
return newPersons()
}
return nil
}


func newPersons() *[]Person {
var models []Person
return &models
}

去抱怨: cannot use newPersons() (type []Person) as type []Model in return argument

我的目标是返回所请求的任何模型类型(无论是 []Person[]FutureModel[]Terminator2000还是 w/e)的一个片段。我遗漏了什么,以及如何正确地实现这样的解决方案?

44758 次浏览

Types T and []T are distinct types and distinct are their methods as well, even when satisfying the same interface. IOW, every type satisfying Model must implement all of the Model's methods by itself - the method receiver can be only one specific type.

This is very similar to a question I just answered: https://stackoverflow.com/a/12990540/727643

The short answer is that you are correct. A slice of structs is not equal to a slice of an interface the struct implements.

A []Person and a []Model have different memory layouts. This is because the types they are slices of have different memory layouts. A Model is an interface value which means that in memory it is two words in size. One word for the type information, the other for the data. A Person is a struct whose size depends on the fields it contains. In order to convert from a []Person to a []Model, you will need to loop over the array and do a type conversion for each element.

Since this conversion is an O(n) operation and would result in a new slice being created, Go refuses to do it implicitly. You can do it explicitly with the following code.

models := make([]Model, len(persons))
for i, v := range persons {
models[i] = Model(v)
}
return models

And as dskinner pointed out, you most likely want a slice of pointers and not a pointer to a slice. A pointer to a slice is not normally needed.

*[]Person        // pointer to slice
[]*Person        // slice of pointers

Maybe this is an issue with your return type *[]Person, where it should actually be []*Person so to reference that each index of the slice is a reference to a Person, and where a slice [] is in itself a reference to an array.

Check out the following example:

package main


import (
"fmt"
)


type Model interface {
Name() string
}


type Person struct {}


func (p *Person) Name() string {
return "Me"
}


func NewPersons() (models []*Person) {
return models
}


func main() {
var p Model
p = new(Person)
fmt.Println(p.Name())


arr := NewPersons()
arr = append(arr, new(Person))
fmt.Println(arr[0].Name())
}

As Stephen already answered the question and you're a beginner I emphasize on giving advises.

A better way of working with go's interfaces is not to have a constructor returning the interface as you might be used to from other languages, like java, but to have a constructor for each object independently, as they implement the interface implicitly.

Instead of

newModel(type string) Model { ... }

you should do

newPerson() *Person { ... }
newPolitician() *Politician { ... }

with Person and Politician both implementing the methods of Model. You can still use Person or Politician everywhere where a Model is accepted, but you can also implement other interfaces.

With your method you would be limited to Model until you do a manual conversion to another interface type.

Suppose I have a Person which implements the method Walk() and a Model implements ShowOff(), the following would not work straight forward:

newModel("person").ShowOff()
newModel("person").Walk() // Does not compile, Model has no method Walk

However this would:

newPerson().ShowOff()
newPerson().Walk()

As others have already answered, []T is a distinct type. I'd just like to add that a simple utility can be used to convert them generically.

import "reflect"


// Convert a slice or array of a specific type to array of interface{}
func ToIntf(s interface{}) []interface{} {
v := reflect.ValueOf(s)
// There is no need to check, we want to panic if it's not slice or array
intf := make([]interface{}, v.Len())
for i := 0; i < v.Len(); i++ {
intf[i] = v.Index(i).Interface()
}
return intf
}

Now, you can use it like this:

ToIntf([]int{1,2,3})

Even if Go's implementation allowed this, it's unfortunately unsound: You can't assign a []Person to a variable of type []Model because a []Model has different capabilities. For example, suppose we also have Animal which implements Model:

var people []Person = ...
var models []Model = people // not allowed in real Go
models[0] = Animal{..} // ???
var person Person = people[0] // !!!

If we allow line 2, then line 3 should also work because models can perfectly well store an Animal. And line 4 should still work because people stores Persons. But then we end up with a variable of type Person holding an Animal!

Java actually allows the equivalent of line 2, and it's widely considered a mistake. (The error is caught at run time; line 3 would throw an ArrayStoreException.)