The chief distinction between the two forms is that they differ not only in syntax but rather in their semantics:
- The "normal" form which uses the
func name (...)
form
creates a regular named function.
- What you call "create a function using
var
" is actually creating a so-called anonymous function and assigning its value to a variable.
The difference is that in Go, anonymous functions behave as closures, while regular functions do not.
A closure is a function which "captures" any variables from its outer lexical scope which are used in the function's body (while not being shadowed by local variables and function arguments there).
The distinction may not be apparent when either form it used at the top level of a package — that is, outside of any function's body, — since the outer scope in this case is the package's global variables, but inside other functions the distinction is apparent: the "normal" form simply cannot be used.
Other distinction is that you can replace the value in a variable containing a function value, while you cannot do the same with the normal function.
Still, if we're talking about the top-level code, the suggestion offered by Flimzy holds: in production Go code, having global variables containing function values is code smell until it can be proved to be otherwise.