String 为什么可以';我是否将字符串作为指定的Go引用附加到字节片?
引自 作为一种特殊情况,将字符串附加到字节片是合法的,如下所示:String 为什么可以';我是否将字符串作为指定的Go引用附加到字节片?,string,go,slice,String,Go,Slice,引自 作为一种特殊情况,将字符串附加到字节片是合法的,如下所示: slice=append([]字节(“hello”),“world”…) 但我发现我不能这样做,因为这段代码: package main import "fmt" func main(){ a := []byte("hello") s := "world" a = append(a, s) //*Error*: can't use s(type string) as type byte in append
slice=append([]字节(“hello”),“world”…)
但我发现我不能这样做,因为这段代码:
package main
import "fmt"
func main(){
a := []byte("hello")
s := "world"
a = append(a, s) //*Error*: can't use s(type string) as type byte in append
fmt.Printf("%s",a)
}
我做错了什么?您需要使用“…”作为后缀,以便将一个切片附加到另一个切片。
像这样:
package main
import "fmt"
func main(){
a := []byte("hello")
s := "world"
a = append(a, s...) // use "..." as suffice
fmt.Printf("%s",a)
}
你可以在这里试试: