开发者

How to copy array into part of another in Go?

I 开发者_Python百科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!


Use the copy built-in function.

package main

func main() {
    largeArray := make([]byte, 1000)
    smallArray := make([]byte, 10)
    copy(largeArray[0:10], smallArray[:])
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜