go语言:多个[]byte数组合并成一个[]byte

    场景:在开发中,要将多个[]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(""))
}
    简直酷毙了吧~~~~~~~~~~要了解语言特性,并进行重构。这就是成功的要素。
原文地址:https://www.cnblogs.com/zsy/p/5935407.html