Golang将指针的两个切片连接起来



在我的GoLang程序中,它调用REST API,我需要收集来自不同的REST API的响应,这些响应返回同一结构的指针切片。我正试图使用append将指针的切片连接起来,结果出现了类似于下面所示的错误。我认为append不支持这样的操作,有其他选择吗?

cannot use response (type []*string) as type *string in append

这里给出了一个我试图证明的问题的操场链接。https://play.golang.org/p/lnzSd2kbht0

package main
import (
"fmt"
)
func main() {
var fruits []*string
response := GetStrings("Apple")
fruits = append(fruits, response...)
response = GetStrings("Banana")
fruits = append(fruits, response...)
response = GetStrings("Orange")
fruits = append(fruits, response...)
if fruits == nil || len(fruits) == 0 {
fmt.Printf("Nil Slice")
} else {
fmt.Printf("Non nil")
fmt.Printf("%v", fruits)
}
}
func GetStrings(input string) []*string {
var myslice []*string
myslice = append(myslice, &input)
return myslice
}

我无法更改RESTneneneba API或函数签名来返回结构本身的切片。

要将一个切片的所有元素附加到另一个切片,请使用:

resultSlice=append(slice1, slice2...)

最新更新