dongre9937 2015-02-08 10:59
浏览 71
已采纳

Golang中的通用方法参数

I need help with making this work for any type.

I have got a function I need to accept other types that have ID property.

I have tried using interfaces but that did not work for my ID property case. Here is the code:

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)
}

I get this

error prog.go:39: cannot use humans (type []Human) as type []Mammal in argument to Count

See Go Playground for more details here http://play.golang.org/p/xzWgjkzcmH

  • 写回答

2条回答 默认 最新

  • doupacan2098 2015-02-08 11:36
    关注

    Use interfaces instead of concrete types, and use embedded interfaces so the common methods do not have to be listed in both types:

    type Mammal interface {
        GetID() int
        GetName() string
    }
    
    type Human interface {
        Mammal
    
        GetHairColor() string
    }
    

    And here is the implementation of these interfaces based on your code which uses embedded types (structs):

    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
    }
    

    But then of course in your Count() function you can only refer to the method and not the field of the implementation:

    IDs[i] = strconv.Itoa(m.GetID())  // Access ID via the method: GetID()
    

    And creating your slices of Mammals and Humans:

    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"},
    }
    

    Here is the complete working code on Go Playground.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算