``` ``` func PossibleMoves（）[8] int {
/ *计算 * /
return Posmoves []
}

func traversal（）{

var currentarray [8] int

copy（Posmoves，currentarray）
}
</ code> </ pre  >

</ div>``````

### 原文

I want to know how to copy elements of one array returned by a function into another array in a different function.

Eg:

`````` func PossibleMoves()[8] int{
/* calculations*/
return Posmoves[]
}

func traversal(){

var currentarray [8] int

copy(Posmoves,currentarray)
}
``````

This shows an error saying undefined Posmoves, How should I correct it...

2个回答

``` ``` var currentarray [8] int = PossibleMoves（）
</ code> </ pre>

currentarray：  = PossibleMoves（）
</ code> </ pre>

var a [8] int
var b =  [8] int {1、2、3、4、5、6、7、8}

fmt.Println（a，b）
a = b
fmt.Println（a，b）

a [  0] = 9
fmt.Println（a，b）
</ code> </ pre>

[0 0 0 0 0 0 0 0]  [1 2 3 4 5 6 7 8]
[1 2 3 4 5 6 7 8] [1  2 3 4 5 6 7 8]
[9 2 3 4 5 6 7 8] [1 2 3 4 5 6 7 8]
</ code> </ pre>

</ div>``````

### 原文

Slices are descriptors (headers pointing to a backing array), not like arrays which are values. An array value means all its elements.

So in order to copy all the elements from one array into another, you don't need any "special" copy function, you just need to assign the array value.

Seriously, this is all what you need:

``````var currentarray [8]int = PossibleMoves()
``````

Or you can use short variable declaration and then it's just

``````currentarray := PossibleMoves()
``````

See this simple example:

``````var a [8]int
var b = [8]int{1, 2, 3, 4, 5, 6, 7, 8}

fmt.Println(a, b)
a = b
fmt.Println(a, b)

a[0] = 9
fmt.Println(a, b)
``````

Output (try it on the Go Playground):

``````[0 0 0 0 0 0 0 0] [1 2 3 4 5 6 7 8]
[1 2 3 4 5 6 7 8] [1 2 3 4 5 6 7 8]
[9 2 3 4 5 6 7 8] [1 2 3 4 5 6 7 8]
``````

As you can see, after the assignment (`a = b`), the `a` array contains the same elements as the `b` array.

And the 2 arrays are distinct / independent: modifying an element of `a` does not modify the array `b`. We modified `a[0] = 9`, `b[0]` still remains `1`.

`Copy` function is used with two slices as arguments `(1 -> dst, 2 -> src)`, then, you must use two slices or convert your `[8]int` arrays to slices, you could do it using `[:]` operator. This operator will return a slice that will have a reference to `[8]int` array.

`Posmoves` is undefined because you didn't define in any place. Then, you could create a global variable:

``````var Posmoves [8]int

func main() {
PossibleMoves()
traversal()

fmt.Println(Posmoves)
}

func PossibleMoves() [8]int {
/* calculations*/
return Posmoves
}

func traversal() {

var currentarray [8]int

copy(Posmoves[:], currentarray[:])
}
``````

Playground

It returns `[0 0 0 0 0 0 0 0]` because both arrays are initialized to zero value (default value).

You could execute `trasversal()` too and this will get `Posmoval array` from `PossibleMoves()`. So:

``````func main() {
traversal()
}

func PossibleMoves() [8]int {
/* calculations*/

var Posmoves [8]int

return Posmoves
}

func traversal() {

var currentarray [8]int

Posmoves := PossibleMoves()

copy(Posmoves[:], currentarray[:])

fmt.Println(currentarray)
}
``````

Playground

The output will be again: `[0 0 0 0 0 0 0 0]`

I hope it helps you! :-)

dongzhi1851 棋子是一个骑士，可能的移动功能以一个骑士的模式计算给定的移动，并且下面的排序功能避免了移动出局，您编写的代码正确，而输入应来自一个 文本文件。 @托尼·维莱纳

dsdv76767671 什么样的棋子是什么？ 如果您的象棋棋子处于“ A1”位置，则其移动方式将为​​“ C2”和“ B3”。 或者，它如何运作机芯？ 有范围吗？ 这种行为正确吗？ play.golang.org/p/XdLlyFczX_

douzhang8840 您可以问与此有关的任何问题，我可以解释，是的，我会等着没有问题@Toni Villena

doqvzh345334 ，我正在查看您的playgolang。 您有些失败，所以很基本...如果您等待，我会给您一个playgolang。 但是，有些事情我不太了解

dougong5817 只是为了澄清..我的输入是在棋盘上的两个位置，我试图使用dfs在两个位置之间找到最短的路径，所以我将得到一个文本文件，其输入内容如：A3，B2 D3，C4等。 错误部分我仍然在@ToniVillena范围之外获得相同的错误索引

dousi8559 ，您的代码会失败，因为您尝试执行strs [0] = currentSource。 并且currentSource初始化为零（“”）。 当执行IsValid（currentSource）时，尝试访问字符串b（currentSource）的0和1位置（b [0]和b [1]），但是currentSource或b的长度为0。请尝试play.golang。 组织/ p / XMbXWDLY9m

dongshai1944 play.golang.org/p/fo5AkWNbEo这是代码，如果您擅长遍历（dfs）并且您认为我的代码在某些时候不合适，请告诉我我很乐意对其进行更改。 @ToniVillena

dsf323233323332 请给我一个playgolang和您的代码，好吗？ 这样会更容易

dt246813579 它给出了一个错误消息，表示恐慌：超出范围的索引@ToniVillena