Creating Python's next() alternative using Go Closures
Posted on 4 June, 2020
If you don't know what next() in python means, the below code illustrates it.
So if you had guess this would print
The
next()
function is used to get the next item in an iterator.
Go doesn't have a next method (nor the concept of iterators actually) so we will try to achieve something similar using Closures.
A closure is implemented through a anonymous(function with no name) function, basically closure is an instance of function.
In Go functions are first class citizens, meaning we can do all sort of things with them, assign them to a variable, pass as an argument to another function.
Below is a naive implementation of how this could look in Go. Ping me if you have a better way to do this ;)
See this demo on Go Playground.
Last updated