Quote from the reference of append
of Go
As a special case, it is legal to append a string to a byte slice, like this:
slice = append([]byte("hello "), "world"...)
But I find I can't do that as this snippet:
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)
}
What have I done wrong?
You need to use "..." as suffix in order to append a slice to another slice. Like this:
package main
import "fmt"
func main(){
a := []byte("hello")
s := "world"
a = append(a, s...) // use "..." as suffice
fmt.Printf("%s",a)
}
You could try it here: http://play.golang.org/p/y_v5To1kiD
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.