Skip to content

Instantly share code, notes, and snippets.

@keshavab
Last active March 31, 2016 10:15
Show Gist options
  • Save keshavab/279e74a2c9b16fbc90c5db33fce080c1 to your computer and use it in GitHub Desktop.
Save keshavab/279e74a2c9b16fbc90c5db33fce080c1 to your computer and use it in GitHub Desktop.
anonymous function
package main
import "fmt"
func printMessage(message string) {
fmt.Println(message)
}
func getPrintMessage() func(string) {
// returns an anonymous function
return func(message string) {
fmt.Println(message)
}
}
func main() {
// named function
printMessage("Hello function!")
// anonymous function declared and called
func(message string) {
fmt.Println(message)
}("Hello anonymous function!")
// gets anonymous function and calls it
printfunc := getPrintMessage()
printfunc("Hello anonymous function using caller!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment