Skip to content
Santekno.com | Level Up Your Engineering Skills
EN
📖 0%
17 Aug 2022 · 3 min read ·Article 14 / 119
Go

Get to know functions, multiple returns, variadic and closures in Golang

IH
Ihsan Arif
Writer at Santekno · Backend Engineer

Second Function

Consists of input, output.

Until now, we can only use the Go program for play functions like this.

go
1  func main() {}

For example, in the previous program we operated on addition and average in an array, so to make it more modular we will change it to a separate function like this.

go
1func average(xs []float64) float64 {
2     total := 0.0
3     for _, v := range xs {
4           total += v
5}
6     return total / float64(len(xs))
7}

So we will try out how a variable can work in related functions and dependencies. Program A

go
1func f() {
2	fmt.Println(x)
3}
4func main() {
5	x := 5
6	f()
7}

Program B

go
1func f(x int) {
2     fmt.Println(x)
3}
4func main() {
5x := 5
6f(x) }

Program C

go
1var x int = 5
2func f() {
3     fmt.Println(x)
4}
5func main() {
6     f()
7}

Program D

go
1func main() {
2	fmt.Println(f1())
3}
4func f1() int {
5	return f2()
6}
7func f2() int {
8	return 1
9}

Multiple Returning

If in other programming you can only return the results of only one data, but in Go, it has its own uniqueness, namely that we can output more than one return. To understand better, see below.

go
 1package main
 2
 3import "fmt"
 4
 5func f() (int, int) {
 6	return 5, 6
 7}
 8func main() {
 9	x, y := f()
10	fmt.Println(x, y)
11}

Variadic Function

What is most special in Go programming is support for Variadic, namely multiple parameters.

go
 1package main
 2
 3import "fmt"
 4
 5func add(args ...int) int {
 6	total := 0
 7	for _, v := range args {
 8		total += v
 9	}
10	return total
11}
12func main() {
13	fmt.Println(add(1, 2, 3))
14}

Closure

Usually we often see javascript or typescript programming. Well, Golang also has sophisticated things like Closure. The following is an example program below.

go
 1package function
 2
 3import "fmt"
 4
 5func main() {
 6	add := func(x, y int) int {
 7		return x + y
 8	}
 9	fmt.Println(add(1, 1))
10}

We can also use sending variables to the closure function, for example like this

go
 1package main
 2
 3import "fmt"
 4
 5func main() {
 6	x := 0
 7	increment := func() int {
 8		x++
 9		return x
10	}
11	fmt.Println(increment())
12	fmt.Println(increment())
13}

Examples of other programs outside of functions we can make like this

go
 1package main
 2
 3import "fmt"
 4
 5func makeEvenGenerator() func() uint {
 6	i := uint(0)
 7	return func() (ret uint) {
 8		ret = i
 9		i += 2
10		return
11	}
12}
13func main() {
14	nextEven := makeEvenGenerator()
15	fmt.Println(nextEven()) // 0
16	fmt.Println(nextEven()) // 2
17	fmt.Println(nextEven()) // 4
18}

Recursion

We can also call the function itself in Go, for example an example program for calculating factorials.

go
 1package main
 2
 3import "fmt"
 4
 5func factorial(x uint) uint {
 6	if x == 0 {
 7		return 1
 8	}
 9	return x * factorial(x-1)
10}
11
12func main() {
13	fmt.Println(factorial(uint(3)))
14}

Defer, Panic, Recovery

defer is a call that will later be terminated. an example can be seen below

go
 1package main
 2
 3import "fmt"
 4
 5func first() {
 6	fmt.Println("1st")
 7}
 8func second() {
 9	fmt.Println("2nd")
10}
11func main() {
12	defer second()
13	first()
14}

Then to define a program that can stop and recover again, we can also use Go. For example, as shown below.

go
1package main
2import "fmt"
3func main() {
4     defer func() {
5           str := recover()
6           fmt.Println(str)
7     }()
8     panic("PANIC")
9}

Related Articles

💬 Comments