Function for copying arrays in Go language

huangapple go评论212阅读模式
英文:

Function for copying arrays in Go language

问题

Go语言中是否有用于将一个数组复制到另一个数组的内置函数?在处理二维(或更高维)数组时,这个方法是否适用?

英文:

Is there any built-in function in Go for copying one array to another?
Will this work in case of two (or more) dimensional arrays?

答案1

得分: 39

Go语言中有一个内置函数可以将一个数组复制到另一个数组中,它就是copy函数。下面是一个示例:

  1. a := []string{
  2. "hello",
  3. "world",
  4. }
  5. b := []string{
  6. "goodbye",
  7. "world",
  8. }
  9. copy(a, b)
  10. // a == []string{"goodbye", "world"}

对于二维(或更高维)数组,copy函数只会进行浅拷贝。下面是一个示例:

  1. a := make([][]string, 10)
  2. b := make([][]string, 10)
  3. for i := range b {
  4. b[i] = make([]string, 10)
  5. for j := range b[i] {
  6. b[i][j] = strconv.Itoa(i + j)
  7. }
  8. }
  9. copy(a, b)
  10. // a和b看起来是一样的
  11. b[1] = []string{"some", "new", "data"}
  12. // b的第二行不同,但a仍然是一样的
  13. b[0][0] = "apple"
  14. // 现在a看起来不同了

如果你想要进行多维数组的深拷贝,目前没有内置的函数可以直接实现。你可以手动进行深拷贝,下面是一个示例:

  1. a := make([][]string, 10)
  2. b := make([][]string, 10)
  3. for i := range b {
  4. b[i] = make([]string, 10)
  5. for j := range b[i] {
  6. b[i][j] = strconv.Itoa(i + j)
  7. }
  8. }
  9. // 手动进行深拷贝
  10. for i := range b {
  11. a[i] = make([]string, len(b[i]))
  12. copy(a[i], b[i])
  13. }
  14. b[0][0] = "apple"
  15. // a仍然是一样的

需要注意的是,我假设你所说的"copy an array"是指"对切片进行深拷贝",因为数组可以使用=运算符进行深拷贝。

英文:

> Is there any built-in function in Go language for copying one array to another?

Yes: http://play.golang.org/p/_lYNw9SXN5

  1. a := []string{
  2. "hello",
  3. "world",
  4. }
  5. b := []string{
  6. "goodbye",
  7. "world",
  8. }
  9. copy(a, b)
  10. // a == []string{"goodbye", "world"}

> Will this work in case of two (or more) dimensional arrays?

copy will do a shallow copy of the rows: http://play.golang.org/p/0gPk6P1VWh

  1. a := make([][]string, 10)
  2. b := make([][]string, 10)
  3. for i := range b {
  4. b[i] = make([]string, 10)
  5. for j := range b[i] {
  6. b[i][j] = strconv.Itoa(i + j)
  7. }
  8. }
  9. copy(a, b)
  10. // a and b look the same
  11. b[1] = []string{"some", "new", "data"}
  12. // b's second row is different; a still looks the same
  13. b[0][0] = "apple"
  14. // now a looks different

I don't think there's a built-in for doing deep-copys of multi-dimensional arrays: you can do it manually like: http://play.golang.org/p/nlVJq-ehzC

  1. a := make([][]string, 10)
  2. b := make([][]string, 10)
  3. for i := range b {
  4. b[i] = make([]string, 10)
  5. for j := range b[i] {
  6. b[i][j] = strconv.Itoa(i + j)
  7. }
  8. }
  9. // manual deep copy
  10. for i := range b {
  11. a[i] = make([]string, len(b[i]))
  12. copy(a[i], b[i])
  13. }
  14. b[0][0] = "apple"
  15. // a still looks the same

edit: As pointed out in the comments, I assumed by "copy an array" you meant "do a deep copy of a slice", as arrays can be deep-copied with the = operator as per jnml's answer (because arrays are value types): http://play.golang.org/p/8EuFqXnqPB

答案2

得分: 22

在Go语言中,复制数组的主要方法是使用赋值运算符=,这对于任何其他类型的值也是如此。

  1. package main
  2. import "fmt"
  3. func main() {
  4. var a, b [4]int
  5. a[2] = 42
  6. b = a
  7. fmt.Println(a, b)
  8. // 2D array
  9. var c, d [3][5]int
  10. c[1][2] = 314
  11. d = c
  12. fmt.Println(c)
  13. fmt.Println(d)
  14. }

输出结果:

  1. [0 0 42 0] [0 0 42 0]
  2. [[0 0 0 0 0] [0 0 314 0 0] [0 0 0 0 0]]
  3. [[0 0 0 0 0] [0 0 314 0 0] [0 0 0 0 0]]

Playground

英文:

The primary "function" for copying an array in Go is the assignment operator =, as it is the case for any other value of any other type.

  1. package main
  2. import "fmt"
  3. func main() {
  4. var a, b [4]int
  5. a[2] = 42
  6. b = a
  7. fmt.Println(a, b)
  8. // 2D array
  9. var c, d [3][5]int
  10. c[1][2] = 314
  11. d = c
  12. fmt.Println(c)
  13. fmt.Println(d)
  14. }

Playground


Output:

  1. [0 0 42 0] [0 0 42 0]
  2. [[0 0 0 0 0] [0 0 314 0 0] [0 0 0 0 0]]
  3. [[0 0 0 0 0] [0 0 314 0 0] [0 0 0 0 0]]

答案3

得分: 10

使用copy函数将切片a的元素复制到数组b中。

  1. a := []int{1, 2, 3}
  2. var b [3]int
  3. fmt.Println("A:", a)
  4. fmt.Println("B:", b)
  5. copy(b[:], a)
  6. fmt.Println("A:", a)
  7. fmt.Println("B2:", b)
  8. b[1] = 9
  9. fmt.Println("A:", a)
  10. fmt.Println("B3:", b)

输出结果:

  1. A: [1 2 3]
  2. B: [0 0 0]
  3. A: [1 2 3]
  4. B2: [1 2 3]
  5. A: [1 2 3]
  6. B3: [1 9 3]
英文:

Use copy http://play.golang.org/p/t7P6IliMOK

  1. a := []int{1, 2, 3}
  2. var b [3]int
  3. fmt.Println("A:", a)
  4. fmt.Println("B:", b)
  5. copy(b[:], a)
  6. fmt.Println("A:", a)
  7. fmt.Println("B2:", b)
  8. b[1] = 9
  9. fmt.Println("A:", a)
  10. fmt.Println("B3:", b)

OUT:

  1. A: [1 2 3]
  2. B: [0 0 0]
  3. A: [1 2 3]
  4. B2: [1 2 3]
  5. A: [1 2 3]
  6. B3: [1 9 3]

huangapple
  • 本文由 发表于 2013年9月1日 23:19:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/18559830.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定