First look at functions in Go

April 26th, 2018 | #go

Recently, I started learning Go. The first thing I took a look at were functions in Go. Functions are central in Go, and have several cool features, like multiple return values. Let me share my first look at functions in Go with you.

Functions

A basic function in Go, that takes two ints and returns their sum as an int, looks like this:

func plus(a int, b int) int {
    return a + b
}

Like many languages, Go requires explicit returns.

Consecutive parameters of the same type

When you have a method with consecutive parameters of the same type, in this case int, you may omit the type name for the parameters with the same type up to the final parameter, which must declare the type:

func plus(a, b, c int) int {
    return a + b + c
}

Calling a function

You call a function in Go just as you would expect:

name(args)

Multiple return values

The past year I coded in Java for 90% of the time. So when I heard that functions in Go could return multiple values, I was like: "What? Do you mean key and value? Like maps?".

Nevertheless, Go has a built-in support for multiple return values. This can be very useful, for example, if you want to return both result and error values from a function.

The (int, int) in the signature of the following function show that the function returns two ints:

func multiple() (int, int) {
    return 1, 2
}

With multiple assignment, you can use the two return values:

a, b := multiple()
fmt.Println(a)
fmt.Println(b)

It is also possible that you only want a subset of the returned values. Go provides a blank identifier (_) which can be assigned or declared with any value of any type to discarde the value harmlessly.

_, c := multiple()
fmt.Println(c)

Variadic functions

A variadic function is a function that accepts a variable number of arguments. One common example is fmt.Println. The following function takes a variable number of ints as arguments:

func total(nums ...int) {
    ...
}

Anonymous functions and closures

Go also supports anonymous functions—a function definition that is not bound to an identifier. In other words, an anonymous function is function which is defined inline without having to name it. Anonymous functions can form closures.

In the following example we see the function intSequence which returns another function that is defined anonymously within the body of intSequence. The function that is returned, closes over variable i to form a closure.

func intSequence() func() int {
    i := 0

    return func() int {
        i++
        return i
    }
}

Recursion

The last thing I will cover in this blogpost is a recursive function. In contrast to iterations, recursion is a method where the solution to a problem depends on solutions to smaller instances of the same problem. A classix example of a recursive procedure is the following function—which calculates the factorial of a natural number:

func factorial(n int) int {
    if n == 0 {
        return 1
    }

    return n * fact(n-1)
}

Keep my blog tuned for more Gopher blogposts. 😉


© 2018 Tonny Garić | With from Nijmegen, The Netherlands