简体   繁体   中英

Using slices and Arrays in go error

I am learning go and I am used to using Java so I am running into errors than in my mind don't seem to be a problem. 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}

    fmt.Println(reverseReverse(f,h))

}
func reverseReverse(first []int, second []int) ([]int, []int){
    //creating temp arrays to hold the traversed arrays before swapping.
    var tempArr1 []int
    var tempArr2 []int
    //count is used for counting up the tempArrays in the correct order in the For loops
    var count = 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]
        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
}

Basically I am trying to write a method that takes two arrays and returns them reversed and swapped.

Ex:

arr1 = {1,2,3}

arr2 = {6,7,8}

should return:

arr1 = {8,7,6}

arr2 = {3,2,1}

My error that I am getting is as such :

src\\main\\goProject.go:35: cannot use first (type [5]int) as type []int in return argument

src\\main\\goProject.go:35: cannot use second (type [5]int) as type []int in return argument

It says: Cannot use f (type [5]int) as type []int on the variables in my print statement.

I had problems before and swapped my tempArrays to be slices, but I don't understand why I am getting this error.

Side note: I tried replacing the parameters array lengths to ... with no luck either:

func reverseReverse(first [...]int, second [...]int) ([]int, []int){

This created the same error as before just says:

f (type [5]int) as type [...]int

So my question is: why am I getting this error? This is all the code I have comment any questions for more info if need be.

Here:

Before I changed the temp array to a slice I had this:

var tempArr1 [len(first)]int
var tempArr2 [len(second)]int

I still get the same error as stated before, but the new error is:

src\\main\\goProject.go:15: non-constant array bound len(first)

src\\main\\goProject.go:16: non-constant array bound len(second)

And I understand it should be constant, but why is using len() not make it constant?

Couple of issues:

  • You have to make a slice with a length of 5 before you can access elements in it. You'll panic if you simply do var tempArr1 []int
  • You can use slices instead of arrays, and your return types will match with []int

You'll find your fix here:
https://play.golang.org/p/5E2hL0796o

Edit: to allow you to keep your data types as an array, simply change your return types to match. Your function signature should look like this:

func reverseReverse(first [5]int, second [5]int) ([5]int, [5]int)

GoPlay here:
https://play.golang.org/p/_eV3Q0kspQ

To answer your question, you cannot have a function take in an array of arbitrary size. You would have to specify the length. There is a fundamental difference in Go for []int and [5]int.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM