Nested functions are allowed in Go. You just need to assign them to local variables within the outer function, and call them using those variables.
Example:
func outerFunction(iterations int, s1, s2 string) int {
someState := 0
innerFunction := func(param string) int {
// Could have another nested function here!
totalLength := 0
// Note that the iterations parameter is available
// in the inner function (closure)
for i := 0; i < iterations; i++) {
totalLength += len(param)
}
return totalLength
}
// Now we can call innerFunction() freely
someState = innerFunction(s1)
someState += innerFunction(s2)
return someState
}
myVar := outerFunction(100, "blah", "meh")
Inner functions are often handy for local goroutines:
func outerFunction(...) {
innerFunction := func(...) {
...
}
go innerFunction(...)
}