我最近嘗試在Go中附加兩個字節數組切片,并遇到了一些奇怪的錯誤。我的代碼是: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)錯誤是:cannot use four (type []uint8) as type uint8 in appendcannot use two[:] (type []uint8) as type uint8 in append考慮到Go切片的魯棒性應該不是問題:http://code.google.com/p/go-wiki/wiki/SliceTricks我在做什么錯,我應該如何追加兩個字節數組?
2 回答

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