在Go中调用append函数时遇到问题
type Dog struct {
color string
}
type Dogs []Dog我想在“狗”后面加上“狗”。
我试过这么做
Dogs = append(Dogs, Dog)但是我得到了这个错误
First argument to append must be slice; have *Dogs编辑:此外,如果我想检查这只狗是否包含颜色“白色”,例如。我该怎么称呼它呢?
if Dog.color.contains("white") {
//then append this Dog into Dogs
}发布于 2017-02-05 18:51:24
狗是一种类型,而不是变量,你的意思可能是:
var Dogs []Dog发布于 2017-02-05 19:04:16
正如朋友们所说的,它不应该是一个类型,下面的例子可能会有所帮助:
// Create empty slice of struct pointers.
Dogs := []*Dog{}
// Create struct and append it to the slice.
dog := new(Dog)
dog.color = "black"
Dogs = append(Dogs, dog)https://stackoverflow.com/questions/42051117
复制相似问题