Function Definitions
In Go functions are declared using the func
keyword:
func myFunction() { fmt.Println("Hello, World!")}
This is similar to JavaScript/TypeScript, but with a few differences in syntax and usage.
Anonymous Functions
Section titled “Anonymous Functions”Like JavaScript/TypeScript, Go also supports anonymous functions.
func main() { myFunc := func() { fmt.Println("Hello from an anonymous function!") } myFunc()}
Immediately Invoked Function Expressions (IIFE)
Section titled “Immediately Invoked Function Expressions (IIFE)”It’s possible to define an anomymous function and immediately execute it, similar to IIFE’s.
func main() { func() { fmt.Println("Hello from an IIFE!") }()}
Common when you deal with more advanced Go features like channels.