Passing an array as an argument in golang

You have defined your function to accept a slice as an argument, while you’re trying to pass an array in the call to that function. There are two ways you could address this:

  1. Create a slice out of the array when calling the function. Changing the call like this should be enough:

    nameReader(a[:])
    
  2. Alter the function signature to take an array instead of a slice. For instance:

    func nameReader(array [3]name) {
        ...
    }
    

    Downsides of this solution are that the function can now only accept an array of length 3, and a copy of the array will be made when calling it.

You can find a more details on arrays and slices, and common pitfalls when using them here

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)