6

I want to write a function that can accept arrays of fixed length, but different arrays have different lengths.

I know that i can pass the slice with arr[:] (the function will accept []T), but is there another way, maybe more efficient?

I'm using a struct that i'd like to mantain with fixed length arrays (for documentation purposes), so using slices everywhere at declaration time is not optimal for my purpose.

3
  • 3
    Just use slice, array is not suitable to be passed as param, it will make a copy, waste space & time, also there are two copy of undering data. Commented Aug 8, 2018 at 9:52
  • 1
    This answer stackoverflow.com/questions/48849493/… might be of help. Commented Aug 8, 2018 at 9:58
  • 1
    Slicing an array is not inefficient at all. See blog.golang.org/slices. Commented Aug 8, 2018 at 10:29

1 Answer 1

6

No there is no way to pass different size arrays, because the length of an array is part of the type.

For example [3]int is a different type then [2]int.

At all in Go it is not recommended to use arrays you should use slices (https://golang.org/doc/effective_go.html#arrays).

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

1 Comment

Another reason not to use array is, when pass array as param, it will make a copy, anyway this won't work.

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.