How can I use Go append with two []byte slices or arrays?

后端 未结 2 813
醉话见心
醉话见心 2020-12-13 17:57

I recently tried appending two byte array slices in Go and came across some odd errors. My code is:

one:=make([]byte, 2)
two:=make([]byte, 2)
one[0]=0x00
one         


        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-13 18:38

    append() takes a slice of type []T, and then a variable number of values of the type of the slice member T. In other words, if you pass a []uint8 as the slice to append() then it wants every subsequent argument to be a uint8.

    The solution to this is to use the slice... syntax for passing a slice in place of a varargs argument. Your code should look like

    log.Printf("%X", append(one[:], two[:]...))
    

    and

    five:=append(three, four...)
    

提交回复
热议问题