programming

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

Second Function

Consists of input, output.

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

  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.

func average(xs []float64) float64 {
     total := 0.0
     for _, v := range xs {
           total += v
}
     return total / float64(len(xs))
}

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

func f() {
	fmt.Println(x)
}
func main() {
	x := 5
	f()
}

Program B

func f(x int) {
     fmt.Println(x)
}
func main() {
x := 5
f(x) }

Program C

var x int = 5
func f() {
     fmt.Println(x)
}
func main() {
     f()
}

Program D

func main() {
	fmt.Println(f1())
}
func f1() int {
	return f2()
}
func f2() int {
	return 1
}

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.

package main

import "fmt"

func f() (int, int) {
	return 5, 6
}
func main() {
	x, y := f()
	fmt.Println(x, y)
}

Variadic Function

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

package main

import "fmt"

func add(args ...int) int {
	total := 0
	for _, v := range args {
		total += v
	}
	return total
}
func main() {
	fmt.Println(add(1, 2, 3))
}

Closure

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

package function

import "fmt"

func main() {
	add := func(x, y int) int {
		return x + y
	}
	fmt.Println(add(1, 1))
}

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

package main

import "fmt"

func main() {
	x := 0
	increment := func() int {
		x++
		return x
	}
	fmt.Println(increment())
	fmt.Println(increment())
}

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

package main

import "fmt"

func makeEvenGenerator() func() uint {
	i := uint(0)
	return func() (ret uint) {
		ret = i
		i += 2
		return
	}
}
func main() {
	nextEven := makeEvenGenerator()
	fmt.Println(nextEven()) // 0
	fmt.Println(nextEven()) // 2
	fmt.Println(nextEven()) // 4
}

Recursion

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

package main

import "fmt"

func factorial(x uint) uint {
	if x == 0 {
		return 1
	}
	return x * factorial(x-1)
}

func main() {
	fmt.Println(factorial(uint(3)))
}

Defer, Panic, Recovery

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

package main

import "fmt"

func first() {
	fmt.Println("1st")
}
func second() {
	fmt.Println("2nd")
}
func main() {
	defer second()
	first()
}

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

package main
import "fmt"
func main() {
     defer func() {
           str := recover()
           fmt.Println(str)
     }()
     panic("PANIC")
}
comments powered by Disqus