如何使用Go的append函数来合并两个[]byte切片或数组?

huangapple go评论95阅读模式
英文:

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

问题

我最近尝试在Go中连接两个字节数组切片,并遇到了一些奇怪的错误。我的代码是:

one := make([]byte, 2)
two := make([]byte, 2)
one[0] = 0x00
one[1] = 0x01
two[0] = 0x02
two[1] = 0x03

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

three := []byte{0, 1}
four := []byte{2, 3}

five := append(three, four)

错误信息如下:

无法将four(类型为[]uint8)用作append中的uint8类型
无法将two[:](类型为[]uint8)用作append中的uint8类型

考虑到Go切片的健壮性,这不应该是个问题:

http://code.google.com/p/go-wiki/wiki/SliceTricks

我做错了什么,应该如何连接两个字节数组?

英文:

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[1]=0x01
two[0]=0x02
two[1]=0x03

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

three:=[]byte{0, 1}
four:=[]byte{2, 3}

five:=append(three, four)

And the errors are:

cannot use four (type []uint8) as type uint8 in append
cannot use two[:] (type []uint8) as type uint8 in append

Which taken into consideration the alleged robustness of Go's slices shouldn't be a problem:

http://code.google.com/p/go-wiki/wiki/SliceTricks

What am I doing wrong, and how should I go about appending two byte arrays?

答案1

得分: 80

你需要使用[]T...作为最后一个参数。

对于你的例子,最后一个参数的切片类型是[]byte,参数后面跟着...

package main

import "fmt"

func main() {
    one := make([]byte, 2)
    two := make([]byte, 2)
    one[0] = 0x00
    one[1] = 0x01
    two[0] = 0x02
    two[1] = 0x03
    fmt.Println(append(one[:], two[:]...))

    three := []byte{0, 1}
    four := []byte{2, 3}
    five := append(three, four...)
    fmt.Println(five)
}

Playground: https://play.golang.org/p/2jjXDc8_SWT

输出:

[0 1 2 3]
[0 1 2 3]
英文:

> The Go Programming Language Specification
>
> Appending to and copying slices
>
> The variadic function append appends zero or more values x to s of
> type S, which must be a slice type, and returns the resulting slice,
> also of type S. The values x are passed to a parameter of type ...T
> where T is the element type of S and the respective parameter passing
> rules apply.
>
> append(s S, x ...T) S // T is the element type of S
>
> Passing arguments to ... parameters
>
> If the final argument is assignable to a slice type []T, it may be
> passed unchanged as the value for a ...T parameter if the argument is
> followed by ....


You need to use []T... for the final argument.

For your example, with the final argument slice type []byte, the argument is followed by ...,

package main

import "fmt"

func main() {
	one := make([]byte, 2)
	two := make([]byte, 2)
	one[0] = 0x00
	one[1] = 0x01
	two[0] = 0x02
	two[1] = 0x03
	fmt.Println(append(one[:], two[:]...))

	three := []byte{0, 1}
	four := []byte{2, 3}
	five := append(three, four...)
	fmt.Println(five)
}

Playground: https://play.golang.org/p/2jjXDc8_SWT

Output:

[0 1 2 3]
[0 1 2 3]

答案2

得分: 10

append()接受一个类型为[]T的切片,然后是切片成员类型T的变量数量。换句话说,如果你将一个[]uint8作为切片传递给append(),那么它希望每个后续的参数都是uint8类型。

解决方法是使用slice...语法将切片传递给可变参数。你的代码应该像这样:

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

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

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...)

huangapple
  • 本文由 发表于 2011年12月11日 10:13:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/8461462.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定