我需要帮助才能使这项工作适用于任何类型。
我有一个函数,我需要接受具有 ID
属性的其他类型。
我尝试过使用接口(interface),但这对我的 ID
属性案例不起作用。这是代码:
package main
import (
"fmt"
"strconv"
)
type Mammal struct{
ID int
Name string
}
type Human struct {
ID int
Name string
HairColor string
}
func Count(ms []Mammal) *[]string { // How can i get this function to accept any type not just []Mammal
IDs := make([]string, len(ms))
for i, m := range ms {
IDs[i] = strconv.Itoa(int(m.ID))
}
return &IDs
}
func main(){
mammals := []Mammal{
Mammal{1, "Carnivorious"},
Mammal{2, "Ominivorious"},
}
humans := []Human{
Human{ID:1, Name: "Peter", HairColor: "Black"},
Human{ID:2, Name: "Paul", HairColor: "Red"},
}
numberOfMammalIDs := Count(mammals)
numberOfHumanIDs := Count(humans)
fmt.Println(numberOfMammalIDs)
fmt.Println(numberOfHumanIDs)
}
我明白了
error prog.go:39: cannot use humans (type []Human) as type []Mammal in argument to Count
有关更多详细信息,请参阅 Go Playground http://play.golang.org/p/xzWgjkzcmH
最佳答案
使用接口(interface)而不是具体类型,并使用embedded interfaces因此不必在两种类型中都列出常用方法:
type Mammal interface {
GetID() int
GetName() string
}
type Human interface {
Mammal
GetHairColor() string
}
下面是基于您使用 embedded types 的代码实现的这些接口(interface)。 (结构):
type MammalImpl struct {
ID int
Name string
}
func (m MammalImpl) GetID() int {
return m.ID
}
func (m MammalImpl) GetName() string {
return m.Name
}
type HumanImpl struct {
MammalImpl
HairColor string
}
func (h HumanImpl) GetHairColor() string {
return h.HairColor
}
当然,在您的 Count()
函数中,您只能引用方法,而不能引用实现的字段:
IDs[i] = strconv.Itoa(m.GetID()) // Access ID via the method: GetID()
并创建您的哺乳动物和人类 slice :
mammals := []Mammal{
MammalImpl{1, "Carnivorious"},
MammalImpl{2, "Ominivorious"},
}
humans := []Mammal{
HumanImpl{MammalImpl: MammalImpl{ID: 1, Name: "Peter"}, HairColor: "Black"},
HumanImpl{MammalImpl: MammalImpl{ID: 2, Name: "Paul"}, HairColor: "Red"},
}
这是 Go Playground 上的完整工作代码.
关于go - Golang 中的泛型方法参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28393166/