Functions are an important element of every programming language because they allow you to break big programs into smaller and more manageable parts. Functions must be as independent from each other as possible and must do one job and only one job well. So, if you find yourselves writing functions that do multiple things, you might consider replacing them by multiple functions instead! The single most popular Go function is main()
, which is used in every independent Go program. You should already know that function definitions begin with the func
keyword.
Anonymous functions can be defined inline without the need for a name, and they are usually used for implementing things that require a small amount of code. In Go, a function can return an anonymous function or take an anonymous function as one of its arguments. Additionally, anonymous functions can be attached to Go variables.
Note that anonymous functions are also called closures, especially in functional...