場景:在開發中,要將多個[]byte數組合並成一個[]byte,初步實現思路如下:
1、獲取多個[]byte長度
2、構造一個二維碼數組
3、循環將[]byte拷貝到二維數組中
package gstore import ( "bytes" ) //BytesCombine 多個[]byte數組合並成一個[]byte func BytesCombine(pBytes ...[]byte) []byte { len := len(pBytes) s := make([][]byte, len) for index := 0; index < len; index++ { s[index] = pBytes[index] } sep := []byte("") return bytes.Join(s, sep) }
結合bytes的特性,可使用join函數進行合並,如下:
package gstore import ( "bytes" ) //BytesCombine 多個[]byte數組合並成一個[]byte func BytesCombine(pBytes ...[]byte) []byte { return bytes.Join(pBytes, []byte("")) }