我最近嘗試在Go中附加兩個(gè)字節(jié)數(shù)組切片,并遇到了一些奇怪的錯(cuò)誤。我的代碼是:one:=make([]byte, 2)two:=make([]byte, 2)one[0]=0x00one[1]=0x01two[0]=0x02two[1]=0x03log.Printf("%X", append(one[:], two[:]))three:=[]byte{0, 1}four:=[]byte{2, 3}five:=append(three, four)錯(cuò)誤是:cannot use four (type []uint8) as type uint8 in appendcannot use two[:] (type []uint8) as type uint8 in append考慮到Go切片的魯棒性應(yīng)該不是問題:http://code.google.com/p/go-wiki/wiki/SliceTricks我在做什么錯(cuò),我應(yīng)該如何追加兩個(gè)字節(jié)數(shù)組?
2 回答

婷婷同學(xué)_
TA貢獻(xiàn)1844條經(jīng)驗(yàn) 獲得超8個(gè)贊
append()
接受一個(gè)類型的切片[]T
,然后接受該切片成員類型的可變數(shù)目的值T
。換句話說,如果將a[]uint8
作為切片傳遞給append()
它,則它希望每個(gè)后續(xù)參數(shù)都是a uint8
。
解決方案是使用slice...
語法來傳遞切片來代替varargs參數(shù)。您的代碼應(yīng)如下所示
log.Printf("%X", append(one[:], two[:]...))
和
five:=append(three, four...)
- 2 回答
- 0 關(guān)注
- 455 瀏覽
添加回答
舉報(bào)
0/150
提交
取消