翻转带for循环逻辑错误的切片

So I am trying to write a method that takes two slices, flips both of them and then gives them to each other.

Ex.

s1 = {1,2,3,4,5}

s2 = {6,7,8,9,10}

Should return:

s1 = {10,9,8,7,6}

s2 = {5,4,3,2,1}

Here is my code:

package main
import(
    "fmt"
)

func main(){
    f:= [5]int{1,2,3,4,5}
    h:= [5]int{6,7,8,9,10}
    var sliceF []int = f[0:5]
    var sliceH []int = h[0:5]

    fmt.Println(reverseReverse(sliceF,sliceH))

}
func reverseReverse(first []int, second []int) ([]int, []int){
    //creating temp arrays to hold the traversed arrays before swapping.
    var tempArr1 []int = first
    var tempArr2 []int = second
    //count is used for counting up the tempArrays in the correct order in the For loops
    var count  int= 0
    //goes through the first array and sets the values starting from the end equal to the temp array
    //which increases normally from left to right.
    for i :=len(first)-1; i>=0;i--{
        tempArr1[count] = first[i]
        fmt.Println(i)
        count++
    }
    count =0
    //same as first for loop just on the second array
    for i :=len(second)-1; i>=0;i--{
        tempArr2[count] = second[i]
        count++
    }
    //trying to replace the values of the param arrays to be equal to the temp arrays
    first=tempArr2
    second = tempArr1
    //returning the arrays
    return first,second
}

When run here is the output:

4

3

2

1

0

[10 9 8 9 10]

[5 4 3 4 5]

*Not I included a print statement in the for loop to check if the index is decreasing properly.

I understand there are better ways to do this but for proof of concept I want to use a for loop.

Any help appreciated. I am new to go and tend to have java habits so I assume somehow my problem is related to that.

This can be done much simpler by realizing there's no need to actually swap the individual elements. Instead, reverse each array and swap their order. Much simpler!

func reverseReverse( a, b []int ) ([]int, []int) {
    return reverse(b), reverse(a)
}

func reverse( a []int ) []int {
    end := len(a) - 1

    // Allocate a new array slice of the same length to copy to.
    ret := make( []int, len(a) )

    // Copy each element of a into ret, reversed.
    for i := range a {
        ret[end-i] = a[i]
    }

    return ret
}

With that revelation, there's little need for the very specialized reverseReverse function. Swap the order yourself.

fmt.Println(reverse(sliceH), reverse(sliceF))

Note that if you just want to take a slice of an array, it's sufficient to write sliceH []int := h[:] without specifying the start and end. The start is assumed to be 0 and the end is the end. Also note there's no need to declare the type, := takes care of that for you.

Even better, you can declare and initialize them directly.

sliceF:= []int{1,2,3,4,5}
sliceH:= []int{6,7,8,9,10}

Short answer:

tempArr1[count] = first[i]

This line is logically identical to:

first[count] = first[i]

Detailed answer:

x := [5]int{} and x := []int{} are in fact two very different assignments. In the first case x is actually a static array. In the second case x is a slice which is in fact a data structure which has a length, capacity and a pointer to the underlying array. Therefore, var tempArr1 []int = first means copy the pointer to the underlying array of first into the tempArr1, so any modification to first[i] will be reflected in tempArr1 and vice versa

For example,

package main

import "fmt"

func reverse(s []int) []int {
    for i := 0; i < len(s)/2; i++ {
        s[i], s[len(s)-1-i] = s[len(s)-1-i], s[i]
    }
    return s
}

func main() {
    s1, s2 := []int{1, 2, 3, 4, 5}, []int{6, 7, 8, 9, 10}
    fmt.Println(s1, s2)
    s1, s2 = reverse(s2), reverse(s1)
    fmt.Println(s1, s2)
}

Output:

[1 2 3 4 5] [6 7 8 9 10]
[10 9 8 7 6] [5 4 3 2 1]