我已經轉到下面的代碼的集合(片):通用方法循環結構
type Foo struct { Id int }
type Bar struct { Id int }
func getIdsFoo(foos []Foo) {
ids = make([]int, len(foos))
// iterate and get all ids to ids array
}
func getIdsBar(bars []Bar) {
ids = make([]int, len(bars))
// iterate and get all ids to ids array
}
有一個聰明的方法來創建一個功能getIds([]Idable)
能接受有方法GetId()
實現的任何結構?
爲什麼downvote?它是一個完美的安納塞爾。只是認爲我需要的是編寫生成器,它會工作:)是否有任何使用嵌入的可能性,以避免一遍又一遍地生成'Len()'和'GetId()? –