duanliang8464 2017-07-25 03:36
浏览 801
已采纳

如何在GO中将二维数组转换为一维数组?

Here are two data structures

result []byte
chunk  [][]byte

"chunk" is initialized as follows

chunk := make([][]byte, 3)
for i := 0 ; i < 5; i++ {
      chunk[i] = data //data is a byte array
}

How can I concatenate chunks into to result[]?

Example

If chunks is "123", "456", "789", then result should be "123456789"

  • 写回答

2条回答 默认 最新

  • douying7289 2017-07-25 09:22
    关注

    You can use the "bytes".Join function from the standard library:

    result := bytes.Join(chunks, nil)
    

    First argument is your slice of slices ([][]byte), second argument is the separator (aka glue). In your case, the separator is an empty slice of bytes (nil works as well).

    In the playground: https://play.golang.org/p/8pquRk7PDo.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了