Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Function Closures in Go (Golang)

Posted on March 6, 2023March 6, 2023 by admin

Table of Contents

  • Overview
  • Code:
    • Example 1
    • Example 2:
    • Example 3:

Overview

Function closures are nothing but an anonymous function that can access variables declared outside the function and also retain the current value of those variables between different function calls. Anonymous functions are functions that are not named.

A closure happens when a function is defined within a different function and the inner function can access the variable of the outer function.

You can read more about closures here

https://en.wikipedia.org/wiki/Closure_(computer_programming)

Code:

Let’s see an example. Three things to notice in Example 1

  • The getModulus function returns a closure. It is assigned to a variable modulus
  • This closure function can access the count variable defined outside its body.
  • The value of the count variable is retained between different function calls of modulus function

Example 1

package main
import (
"fmt"
)
func main() {
modulus := getModulus()
modulus(-1)
modulus(2)
modulus(-5)
}
func getModulus() func(int) int {
count := 0
return func(x int) int {
count = count + 1
fmt.Printf("modulus function called %d times\n", count)
if x < 0 {
x = x * -1
}
return x
}
}

Output:

modulus function called 1 times
modulus function called 2 times
modulus function called 3 times

Example 2:

Below is also another example of a closure function. The function is able to access the valueOutside variable.

package main
import "fmt"
func main() {
valueOutside := "somevalue"
func() {
fmt.Println(valueOutside)
}()
}

Output:

somevalue

Example 3:

In the below example, the closure function is able to access the count variable, as well as the value of the count variable, which is retained between different function calls.

package main
import "fmt"
func main() {
count := 0
for i := 1; i <= 5; i++ {
func() {
count++
fmt.Println(count)
}()
}
}

Output

1
2
3
4
5

Popular Articles

Golang Comprehensive Tutorial Series

All Design Patterns in Go (Golang)

Slice in golang

Variables in Go (Golang) – Complete Guide

OOP: Inheritance in GOLANG complete guide

Using Context Package in GO (Golang) – Complete Guide

All data types in Golang with examples

Understanding time and date in Go (Golang) – Complete Guide

©2023 Welcome To Golang By Example | Design: Web XP