go-examples

Get Set Learn Go


Project maintained by sagar-jadhav Hosted on GitHub Pages — Theme by mattgraham

Closure

Example demonstrates how to declare and use the function inside function in Go programming language. Closures are one of the key aspects of functional programming. Click here to learn more

func main() {
	fmt.Println(outerFuncOne())
	fmt.Println(outerFuncTwo())
	returnInnerFunction := returnFunction()
	fmt.Println(returnInnerFunction())
}

func outerFuncOne() string {
	message := "Hello"

	innerFuncOne := func(message string) (greetingMessage string) {
		greetingMessage = message + "-" + "World!"
		return greetingMessage
	}

	return innerFuncOne(message)
}

/*
 * Inner functions have access to local variables
 * define inside the outer function
 */
func outerFuncTwo() int {
	x := 1

	innerFuncTwo := func() {
		x += 1
	}

	innerFuncTwo()
	return x
}

/*
 * Function can also return another function similar
 * to variables.
 */
func returnFunction() func() string {
	message := "Greetings from "
	return func() string {
		message += "Go!"
		return message
	}
}

Ouput

Hello-World!
2
Greetings from Go!
Try It Out Source Code

Contributors

« Home Page Previous « Variadic Functions Next » Recursion