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

Go Language Functions

Go Language Functions

A function is a basic block of code that performs a task.

A Go program must have at least one main() function.

You can use functions to divide different functionality; logically, each function performs a specified task.

A function declaration tells the compiler the function's name, return type, and parameters.

The Go standard library provides a variety of built-in functions. For example, the len() function can accept different types of arguments and return the length of that type. If we pass a string, it returns the length of the string; if we pass an array, it returns the number of elements contained in the array.


Function definition

The format of a Go function definition is as follows:

func function_name( [parameter list] ) [return_types] {

   函数体

}

Function definition analysis:

Example

The following example is the code for the max() function, which takes two integer parameters num1 and num2 and returns the maximum of the two:

Example

/* The function returns the maximum of two numbers */

func

max

(

num1

,

num2

int

)

int

{

   

/* Declare local variables */

   

var

result

int

   

if

(

num1 > num2

)

{

      result

=

num1

   

}

else

{

      result

=

num2

   

}

   

return

result

}

Function call

When you create a function, you define what it needs to do; you execute the specified task by calling the function.

Call a function, pass arguments to it, and return a value, for example:

Example

package

main

import

"fmt"

func

main

()

{

   

/* Define local variables */

   

var

a

int

=

100

   

var

b

int

=

200

   

var

ret

int

   

/* Call the function and return the maximum value */

   ret

=

max

(

a

,

b

)

   fmt

.

Printf

(

"The maximum value is: %d\n"

,

ret

)

}

/* The function returns the maximum of two numbers */

func

max

(

num1

,

num2

int

)

int

{

   

/* Define local variables */

   

var

result

int

   

if

(

num1 > num2

)

{

      result

=

num1

   

}

else

{

      result

=

num2

   

}

   

return

result

}

In the above example, the main() function calls the max() function, and the execution result is:

最大值是 : 200


Functions returning multiple values

Go functions can return multiple values, for example:

Example

package

main

import

"fmt"

func

swap

(

x

,

y

string

)

(

string

,

string

)

{

   

return

y

,

x

}

func

main

()

{

   a

,

b

:=

swap

(

"Google"

,

"Runoob"

)

   fmt

.

Println

(

a

,

b

)

}

The example above produces:

Runoob Google


Function parameters

If a function uses parameters, those variables are called the function's formal parameters.

Formal parameters are like local variables defined within the function body.

When calling a function, parameters can be passed in two ways:

Passing types Description
Pass by value Pass-by-value means copying the actual argument and passing the copy into the function when calling it, so any modifications to the parameter inside the function will not affect the actual argument.
Pass by reference Pass-by-reference means passing the address of the actual argument to the function when calling it, so any modifications to the parameter inside the function will affect the actual argument.

By default, Go uses pass-by-value, meaning the actual argument is not affected during the call.


Function usage

Function usage Description
A function as an argument to another function After a function is defined, it can be passed as an argument to another function
Closures A closure is an anonymous function that can be used in dynamic programming
Method A method is a function that includes a receiver