GoLang Tutorial - Closures and Anonymous Functions
Regular function
The code below also has a regular function (with its name):
Anonymous function
Let's add an anonymous function (without any function name):
Returning an anonymous function
Now we want to add a regular function returning an anonymous function:
Let's look into the anonymous function more closely:
Note that the () means call and execute the anonymous function. If we write only func(){} without () in func(){}(), we only declare the anonymous function without calling it.
go supports anonymous functions which can form closures.
We can define a function inline without having to name it.
Go functions may be closures.
As we can see from the output, it printed 1, 2, and then 3, and so on. This means the int_seq() is keeping track of the values of i even though it's gone out of scope (outside its function body). That's the key point of a closure.
A closure is a function value (next_int()) that references variables (i) from outside its body (int_seq()). But still remembers the value. The function may access and assign to the referenced variables; in this sense the function is "bound" to the variables.
Let's create another separate instance of the function, int_seq(). In the following code, it's next_int2:
We see the two instances are independent of each other, they are separate instances!
Ref:
Closures - Go Lang Programming Tutorial: Part 15
As an another sample for the closures, in the following code, the fiboi() is a function that returns a function that returns an int:
The code: fibonacci-sequence.go
Note that to get the output value from the function we need to put the returned function into a loop.
Go Tutorial
- GoLang Tutorial - HelloWorld
- Calling code in an external package & go.mod / go.sum files
- Workspaces
- Workspaces II
- Visual Studio Code
- Data Types and Variables
- byte and rune
- Packages
- Functions
- Arrays and Slices
- A function taking and returning a slice
- Conditionals
- Loops
- Maps
- Range
- Pointers
- Closures and Anonymous Functions
- Structs and receiver methods
- Value or Pointer Receivers
- Interfaces
- Web Application Part 0 (Introduction)
- Web Application Part 1 (Basic)
- Web Application Part 2 (Using net/http)
- Web Application Part 3 (Adding "edit" capability)
- Web Application Part 4 (Handling non-existent pages and saving pages)
- Web Application Part 5 (Error handling and template caching)
- Web Application Part 6 (Validating the title with a regular expression)
- Web Application Part 7 (Function Literals and Closures)
- Building Docker image and deploying Go application to a Kubernetes cluster (minikube)
- Serverless Framework (Serverless Application Model-SAM)
- Serverless Web API with AWS Lambda
- Arrays vs Slices with an array left rotation sample
- Variadic Functions
- Goroutines
- Channels ("<-")
- Channels ("<-") with Select
- Channels ("<-") with worker pools
- Defer
- GoLang Panic and Recover
- String Formatting
- JSON
- SQLite
- Modules 0: Using External Go Modules from GitHub
- Modules 1 (Creating a new module)
- Modules 2 (Adding Dependencies)
- AWS SDK for Go (S3 listing)
- Linked List
- Binary Search Tree (BST) Part 1 (Tree/Node structs with insert and print functions)
- Go Application Authentication I (BasicAuth, Bearer-Token-Based Authentication)
- Go Application Authentication II (JWT Authentication)
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization