Functions can take 0 or more arguments and parameter types come after the parameter name.
Return types are specified after the closing bracket of the parameters.
b are of the same type,
int, we can combine their type declaration into the following:
Unlike languages like C, in Go, we can return multiple results.
Note: However, if you only need some of the return values, you may end up with unused variables. In those case, we can ignore some of them by using the
init() is a function which you can define to run code before that of the
init() is the only function which you can define multiple times. These
init() statements will be run in sequence.
In Go, functions are values as well which means we can pass them around.
In the above example, we defined a function called
saySomething which in turn takes in a function that accepts a
string and returns a
main function, we called
saySomething and passed
saySomethingElse as a function argument to it.
What is a closure?
According to this Stack Overflow answer, a closure is a persistent local variable scope. And that is pretty accurate. Essentially, a closure allows us to make references to variables even though the code execution is outside of the block where the variables were defined in.