Last active
March 31, 2016 10:15
-
-
Save keshavab/279e74a2c9b16fbc90c5db33fce080c1 to your computer and use it in GitHub Desktop.
anonymous function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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