Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function which accepts array of arbitrary size as argument (is it possible in Golang?)

Q: Is there a way, in golang, to define a function which accepts an array of arbitrary length as argument?

e.g.,

function demoArrayMagic(arr [magic]int){
....
}

I have understood that in golang, array length is part of the variable type, for this reason the following function is not going to accept one arbitrary array as input

function demoArray(arr [2]int){
....
}

This function is not going to compile with arrInput [6]int as input--i.e., demoArray(arrInput) will fail to compile.

Also the following function, which accepts a slice argument, does not accept arrays as arguments (different types, as expected):

function demoSlice(arr []int){
....
}

i.e., demoSlice(arrInput) does not compile, expects a slice not an array.

The question is, is there a way to define a function that takes arrays of arbitrary length (arrays, NOT slice)? It looks quite strange and limiting for a language to impose this constraint.

The question makes sense independently from the motivation, but, in my case, the reason behind is the following. I have a set of functions which takes as arguments data structures of type [][]int. I noticed that GOB serialization for them is 10x slower (MB/s) than other data structures I have. I suppose that may be related to the chain of derefencing operations in slices. Moving from slices to array--i.e.,defining objects of type [10000][128]int--may improve situation (I hope).

Regards

P.s: I remind now that Go, passes/uses things 'by value', using arrays may be overkill cause golang is going to copy them lot of times. I think I'll stay with slices and I'll try to understand GOB internals a bit.

like image 870
Fabiano Tarlao Avatar asked May 16 '18 11:05

Fabiano Tarlao


People also ask

How do you pass an array in a function argument in Golang?

Explanation: In the above example, we have a function named as myfun() which accept an array as an argument. In the main function, we passed arr[6] of int type to the function with the size of the array and the function return the average of the array.

Is array passed by reference in Golang?

In Go, there is no such thing as passing by reference. Everything is passed by value. If you assign the value of an array to another variable, the entire value is copied. In memory allocation, and in function, the array is actually a really simple data type, and works in much the same way as structs.

How do I return an array from a function in Golang?

In the main() function, we created two arrays of integers intArr, retArr and then we read elements from the user. After that, we passed the created array to the GetArray() function, The GetArray() function modifies the value of the passed array and then returns the modified array to the calling function.


1 Answers

There is not. Go does not support generics.

The only way would be to use interface{}, but that would allow to pass a value of any type, not just arrays of your desired type.

Arrays in Go are "secondary". The solution is to use slices for your requirement.

One thing to note here is that you may continue to use arrays, and only slice them when you want to pass them to this function, e.g.:

func main() {
    a1 := [1]int{1}
    demo(a1[:])

    a2 := [2]int{1, 2}
    demo(a2[:])
}

func demo(s []int) {
    fmt.Println("Passed:", s)
}

Output of the above (try it on the Go Playground):

Passed: [1]
Passed: [1 2]
like image 67
icza Avatar answered Sep 30 '22 05:09

icza