TheDeveloperBlog.com


Golang Func Examples

Func. A function does something. It computes the rotation of a planet. It multiplies x. In Go a func is versatile: it receives, and returns, any number of values.


With multiple return values, our methods can be more clearly written. In Go, return values are handled similarly to parameters. They have names and positions.


An example. Let us start with a simple example—we create a func named "display" that receives two arguments of type int. It returns nothing.

Void: This is a void method. But there is no keyword void we use—Go just omits the return type.

Types: Both arguments (apples, oranges) to this method are of type int. The int is specified after the argument name.

Based on:

Golang 1.4

Golang program that uses func with two arguments

package main

import "fmt"

func display(apples int, oranges int) {
    // Display count of apples and oranges.
    fmt.Println(apples)
    fmt.Println(oranges)
}

func main() {
    // Call display method.
    display(10, 12)
}

Output

10
12

Multiple return values. Sometimes a method has no return value. But often we have one or more things to return—new objects, numbers, results.

Syntax: We specify this method, firstAndLast, has two return values. These are (int, int): specified after the argument.

Return: We can return two arguments by using a return-statement with a comma in it.

Here: In main we pass a slice to firstAndLast, and it returns the first and last elements in the slice.

Slice
Golang program that uses multiple return values

package main

import "fmt"

func firstAndLast(items []int) (int, int) {
    // Return two values.
    return items[0], items[len(items) - 1]
}

func main() {
    data := []int{5, 50, 500, 5000}

    // Assign values to result of method.
    first, last := firstAndLast(data)

    // Display results.
    fmt.Println(first)
    fmt.Println(last)
}

Output

5
5000

Named return values. Optionally we can provide names to return values. In the method, we can use those named values like variables or arguments.

Default: The default value for an int return value is 0. So they do not need to be assigned before returning them again.

Program: Here we enhance firstAndLast so that it sets the "first" and "last" arguments only if the slice has at least two elements.

Golang program that uses named return values, defaults

package main

import "fmt"

func firstAndLast(items []int) (first int, last int) {
    // If slice is at least two elements, set first and last.
    // ... Otherwise, leave the return values as zero.
    if len(items) >= 2 {
	first = items[0]
	last = items[len(items)-1]
    }
    return first, last
}

func main() {
    // For a zero-element slice, both return values are 0.
    data := []int{}
    fmt.Println(firstAndLast(data))

    // The first and last values are set.
    data = []int{9, 8, 7, 6}
    fmt.Println(firstAndLast(data))
}

Output

0 0
9 6

Funcs are powerful. Their multiple return values are useful for concurrency in programs. A complex computation, run on a thread, often has more than one thing to return.