duan48961411
2011-08-31 06:03
浏览 1.1k
已采纳

如何在Go中将数组复制到另一个数组中?

I am new to Go, and would like to copy an array (slice) into part of another. For example, I have a largeArray [1000]byte or something and a smallArray [10]byte and I want the first 10 bytes of largeArray to be equal to the contents of smallArray. I have tried:

largeArray[0:10] = smallArray[:]

But that doesn't seem to work. Is there a built-in memcpy-like function, or will I just have to write one myself?

Thanks!

图片转代码服务由CSDN问答提供 功能建议

我是Go语言的新手,想将一个数组(切片)复制到另一个数组中。 例如,我有一个largeArray [1000] byte之类的东西和一个smallArray [10] byte,我希望largeArray的前10个字节等于smallArray的内容。 我已经尝试过:

  largeArray [0:10] = smallArray [:] 
   
 
 

但这不 似乎行得通。 有内置的类似memcpy的函数,还是我只需要自己写一个?

谢谢!

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dourong4031 2011-08-31 06:18
    已采纳

    Use the copy built-in function.

    package main
    
    func main() {
        largeArray := make([]byte, 1000)
        smallArray := make([]byte, 10)
        copy(largeArray[0:10], smallArray[:])
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题