- Go - Home
- Go - Overview
- Go - Environment Setup
- Go - Program Structure
- Go - Basic Syntax
- Go - Data Types
- Go - Variables
- Go - Constants
- Go - Identifiers
- Go - Keywords
- Go - Operators
- Go - Arithmetic Operators
- Go - Assignment Operators
- Go - Relational Operators
- Go - Logical Operators
- Go - Bitwise Operators
- Go - Miscellaneous Operators
- Go - Operators Precedence
- Go Decision Making
- Go - Decision Making
- Go - If Statement
- Go - If Else Statement
- Go - Nested If Statements
- Go - Switch Statement
- Go - Select Statement
- Go Control Flow Statements
- Go - For Loop
- Go - Nested for Loops
- Go - Break Statement
- Go - Continue Statement
- Go - Goto Statement
- Go Functions
- Go - Functions
- Go - Call by Value
- Go - Call by Reference
- Go - Functions as Values
- Go - Function Closure
- Go - Function Method
- Go - Anonymous function
- Go Strings
- Go - Strings
- Go - String Length
- Go - String Concatenation
- Go - Compare Strings
- Go - Split String
- Go - Substring Extraction
- Go - String Replacement
- Go - String Interpolation
- Go - Parse Date Strings
- Go Arrays
- Go - Arrays
- Go - Multidimensional Arrays
- Go - Multidimensional Arrays
- Go - Passing Arrays to Functions
- Go - Pointers
- Go - Pointers
- Go - Array of pointers
- Go - Pointer to pointer
- Go - Passing pointers to functions
- Go Advanced Control Structures
- Go - Scope Rules
- Go - Dereferencing Pointer
- Go - Structures
- Go - Slice
- Go - Slice of Slices
- Go - Range
- Go - Maps
- Go - Recursion
- Go - Type Casting
- Go - Interfaces
- Go - Type Assertion
- Go - Error Handling
- Go - Concurrency
- Go - Regular Expression
- Go - Inheritance
- Go - Packages
- Go - Templates
- Go - Reflection
- Go - Generics
- Go File Handling
- Go - Read File By Word
- Go - Read File By Line
- Go - Read CSV Files
- Go - Delete File
- Go - Rename & Move File
- Go - Truncate a File
- Go - File Read-Write Mode W/O Truncation
- Go Miscellaneous
- Go - defer Keyword
- Go - Fmt Package
- Go - Zero Value
- Go - Import
Go - Function Closure
Go function closure is a function value that references variables from outside its body. The closures bind these variables and make them accessible even after closing the outer function.
Creating a Closure in Go
A closure is created when an inner function captures and retains access to the variables of its enclosing function. You can create a closure by following the below syntax:
// Creation
counter := func() func() int {
count := 0
return func() int {
count++
return count
}
}
// Use
increment := counter()
Here, the counter function defines a local variable count, and the returned inner function, which will be known as closure, has access to count and modifies it on each call.
Example to Create a Simple Closure
In the following example we are creating a simple closure:
package main
import "fmt"
func main() {
// First, define an outer function
updateCounter := func() func() int {
// define a local variable inside the function
count := 100
return func() int {
count++
return count
}
}
// Now, creating a closure
increment := updateCounter()
// Using (calling) the closure
fmt.Println(increment())
fmt.Println(increment())
}
When the above code is compiled and executed, it produces the following result −
101 102
Passing Values into Closures
You can pass values into the closure while calling the function; you need to define the argument type during defining the nested function.
Example
The following example demonstrates passing the values to a closure:
package main
import "fmt"
func main() {
// Define an outer function that accepts a value
updateCounter := func(initial int) func() int {
count := initial // Initialize count with the passed value
return func() int {
count++
return count
}
}
// Create a closure with an initial value
inc_x := updateCounter(100)
// Use the closure
fmt.Println(inc_x())
fmt.Println(inc_x())
// Create another closure with an initial value
inc_y := updateCounter(200)
// Use the closure
fmt.Println(inc_y())
fmt.Println(inc_y())
}
When the above code is compiled and executed, it produces the following result −
101 102 201 202