Golang: Function

By Xah Lee. Date: . Last updated: .

Function

package main

import "fmt"

// ff is my function name
func ff(x int, y int) int {
        return x + y
}

func main() {
        fmt.Println(ff(3, 4))
}

// print 7

Note: type specification comes after the variable name.

When two or more consecutive named function parameters share a type, you can omit the type except the last one.

package main

import "fmt"

func ff(x, y int) int {
        return x + y
}

func main() {
        fmt.Println(ff(3,4))
}

Multiple Return Values

A function can return more than 1 value.

package main

import "fmt"

// returns 2 values
func ff(x, y int) (int, int) {
        return x, y
}

func main() {
        var a, b = ff(3, 4)
        fmt.Println(a, b)
}

// prints 3 4

Named Return Values

Return values can be named. If so, they are treated as variables defined at the top of the function.

package main

import "fmt"

func gg(n int) (m int) {
        m = n + 1
        return m
}

func main() {
        fmt.Println(gg(5))
}

// 6

Function is a Value

Function is a value. It can be assigned to a variable, passed as argument to a function, or returned by a function.

package main

import "fmt"

func main() {

        // assign a function to a variable
        var hh = func() int {
                return 3
        }
        fmt.Println(hh()) // 3
}

Nested Function

To define a function inside a function, declare a variable and give it a function value.

package main

import "fmt"

func main() {
        // nested function, must use var
        var ff = func(x int) int {
                return x + 1
        }
        fmt.Println(ff(3)) // 4
}

Function Returns a Function

Returns a function:

package main

import "fmt"

// ff returns a function gg
func ff(x int) func(int) int {
        var gg = func(y int) int { return y + x }
        return gg
}

func main() {

        fmt.Println(ff(1)(2))

}

Function as Argument

package main

import "fmt"

func aa(n int) int {
        return n + 1
}

// ff takes a function arg
func ff(gg func(int) int) int {
        return gg(1)
}

func main() {

        fmt.Println(ff(aa)) // 2

}

closure

golang's function also has closure.

It basically means, the local variables of the function do not necessarily go away when the function is exited. The variables maintain their values if the variables are still used (for example, when the variable is used by a inner function that is returned). (it's like using global variables, except they are not global variables.)

package main

import "fmt"

// return a closure
func make_add_1() func(int) int {
        var i = 0

        var gg = func(x int) int {
                i += x
                return i
        }

        // the returned function gg, is called a “closure”
        return gg
}

func main() {
        var f = make_add_1()

        fmt.Println(f(1)) // 1
        fmt.Println(f(1)) // 2
        fmt.Println(f(1)) // 3

}

Reference

The Go Programming Language Specification - The Go Programming Language#Function_types