Gaaker Go

Golang Functions

The keyword func is used to declare functions. A function consists of name, parameters, and return value.

The function can have zero or more parameters, the parameters need to declare the data type, and multiple parameters are separated by commas.

The following is a sample code description:

Custom function name. Note that the function name cannot start with a number and is case sensitive.
a int, b int
The function’s parameters. When multiple consecutive parameter types are the same, you can only declare the last one.
Define the type of return value.
Return the result explicitly.

The following is a complete code example.

package main

import "fmt"

func sum(a, b int) int {
	return a + b

func main() {
	fmt.Println("1+1=", sum(1,1))
    fmt.Println("2+2=", sum(2,2))
    fmt.Println("4+2=", sum(4,2))

Below is the program output:

1+1= 2
2+2= 4
4+2= 6
< Functions Functions >