Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 为什么可以';我是否将字符串作为指定的Go引用附加到字节片?_String_Go_Slice - Fatal编程技术网

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)
}
你可以在这里试试: