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

如何在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 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题