Go 使用指向切片的指针进行切片
我正在尝试使用以下代码修改另一个函数中的切片:Go 使用指向切片的指针进行切片,go,Go,我正在尝试使用以下代码修改另一个函数中的切片: type DT struct { name string number int } func slicer(a *[]DT) { tmp := *a var b []DT b = append(b, tmp[:1], tmp[2:]) *a = b } func main() { o1 := DT { name: "name-1", number: 1,
type DT struct {
name string
number int
}
func slicer(a *[]DT) {
tmp := *a
var b []DT
b = append(b, tmp[:1], tmp[2:])
*a = b
}
func main() {
o1 := DT {
name: "name-1",
number: 1,
}
o2 := DT {
name: "name-2",
number: 2,
}
o3 := DT {
name: "name-3",
number: 3,
}
b := make([]DT, 0)
b = append(b, o1)
b = append(b, o2)
b = append(b, o3)
slicer(&b)
fmt.Println(b)
}
我想要的是,切片的第一个和最后一个元素。但是,在这样做的过程中,我得到了以下错误:
cannot use tmp[:1] (type []DT) as type DT in append
我是一个比较新的围棋语言,所以请引导我通过这一个 您应该使用运算符
…
将切片转换为可变参数列表
b = append(b, tmp[:1]...)
b = append(b, tmp[2:]...)
您应该使用运算符
…
将切片转换为可变参数列表
b = append(b, tmp[:1]...)
b = append(b, tmp[2:]...)
你能解释一下它的作用吗。它实际上已经修好了!你能解释一下它的作用吗。它实际上已经修好了!