1

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 Answers 2

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! :-)

Sign up to request clarification or add additional context in comments.

9 Comments

It gives a error saying panic:index of out range @ToniVillena
Could you give me a playgolang with your code, please? It would be easier
play.golang.org/p/fo5AkWNbEo here is the code, also if your good at traversals(dfs)and you think my code is not right at some point, please let me know I am happy to change it. @ToniVillena
@Riya , your code has that fail because you try to do strs[0] = currentSource. And currentSource is initialized to zero (""). When you execute IsValid(currentSource), you try to access to 0 and 1 position of string b (currentSource) (b[0] and b[1]), but length of currentSource or b is 0. Try this play.golang.org/p/XMbXWDLY9m
Just to Clarify..My input is two positions on the chessboard, and im trying to find the shortest path between the two using dfs, so I will have text file that has input like :A3,B2 D3,C4 etc. Coming to the error part I'm still getting the same error index out of range @ToniVillena
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.