Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Passing Arrays to Functions in Go

Passing Arrays to Functions in Go

Go Language ArraysGo Language Arrays

Arrays in Go are value types, so when an array is passed to a function, a copy of the array is actually passed.

If you want to pass an array argument to a function, you need to declare the parameter as an array in the function definition. We can declare it in the following two ways:

Method 1

The parameter specifies the array size:



func myFunction(param [10]int) {

    ....

}

Method 2

The parameter does not specify the array size:



func myFunction(param []int) {

    ....

}

If you want to modify the original array inside a function, you can do so by passing a pointer to the array.

Example

Let's look at the following example, where the function takes an integer array parameter, another parameter specifies the number of array elements, and it returns the average value:

Example

func

getAverage

(

arr

[]

int

,

size

int

)

float32

{

   

var

i

int

   

var

avg

,

sum

float32

 

   

for

i

=

0

;

i

< size

;

++

i

{

      sum

+=

arr

[

i

]

   

}

   avg

=

sum

/

size

   

return

avg

;

}

Next, let's call this function:

Example

package

main

import

"fmt"

func

main

()

{

   

/* array length is 5 */

   

var

 balance

=

[

5

]

int

{

1000

,

2

,

3

,

17

,

50

}

   

var

avg

float32

   

/* array passed to the function as an argument */

   avg

=

getAverage

(

balance

,

5

)

;

   

/* print the returned average value */

   fmt

.

Printf

(

"average value is: %f "

,

avg

);

}

func

getAverage

(

arr

[

5

]

int

,

size

int

)

float32

{

   

var

i

,

sum

int

   

var

avg

float32

 

   

for

i

=

0

;

i

< size

;

i

++

{

      sum

+=

arr

[

i

]

   

}

   avg

=

float32

(

sum

)

/

float32

(

size

)

   

return

avg

;

}

The output of the above example is:

平均值为: 214.399994

In the example above, the parameter we used does not specify the array size.

Floating-point calculations may have some deviation in output; you can also convert to an integer to set the precision.

Example

package

main

import

(

   

"fmt"

)

func

main

()

{

    a

:=

1

.

69

    b

:=

1

.

7

    c

:=

a

b      

// the result should be 2.873

    fmt

.

Println

(

c

)

 

// the output is 2.8729999999999998

}

Set a fixed precision:

Example

package

main

import

(

   

"fmt"

)

func

main

()

{

    a

:=

1690

         

// represents 1.69

    b

:=

1700

         

// represents 1.70

    c

:=

a

b          

// the result should be 2873000, representing 2.873

    fmt

.

Println

(

c

)

     

// internal encoding

    fmt

.

Println

(

float64

(

c

)

/

1000000

)

// display

}

If you want to modify the original array inside a function, you can do so by passing a pointer to the array.

The following example demonstrates how to pass an array to a function, where the function accepts an array and a pointer to the array as parameters:

Example

package

main

import

"fmt"

// the function accepts an array as a parameter

func

modifyArray

(

arr

[

5

]

int

)

{

   

for

i

:=

0

;

i

<

len

(

arr

);

i

++

{

        arr

[

i

]

=

arr

[

i

]

2

   

}

}

// the function accepts a pointer to an array as a parameter

func

modifyArrayWithPointer

(

arr

[

5

]

int

)

{

   

for

i

:=

0

;

i

<

len

(

arr

);

i

++

{

       

(

arr

)[

i

]

=

(

arr

)[

i

]

2

   

}

}

func

main

()

{

   

// create an integer array with 5 elements

    myArray

:=

[

5

]

int

{

1

,

2

,

3

,

4

,

5

}

    fmt

.

Println

(

"Original Array:"

,

myArray

)

   

// pass the array to the function, but the original array values are not modified

    modifyArray

(

myArray

)

    fmt

.

Println

(

"Array after modifyArray:"

,

myArray

)

   

// pass a pointer to the array to the function, so the original array values can be modified

    modifyArrayWithPointer

(

&

myArray

)

    fmt

.

Println

(

"Array after modifyArrayWithPointer:"

,

myArray

)

}

In the example above, the modifyArray function accepts an array and tries to modify its values, but after being called in the main function, the original array is not modified. In contrast, the modifyArrayWithPointer function accepts a pointer to an array and modifies the original array's values through the pointer.

The output of the above example is:

Original Array: [1 2 3 4 5]

Array after modifyArray: [1 2 3 4 5]

Array after modifyArrayWithPointer: [2 4 6 8 10]

Go Language ArraysGo Language Arrays