Overview of Functions
This lesson explains how to write a simple function in Go.
Functions
The simplest function declaration has the format:
func functionName()
Between the mandatory parentheses ( ) no, one, or more parameters (separated by ,) can be given as input to the function. After the name of each parameter variable must come its type.
The main function as a starting point is required (usually the first function), otherwise the build-error: undefined: main.main
occurs. The main
function has no parameters and no return type (in contrary to the C-family) otherwise, you get the build-error: func main must have no arguments and no return values
.
When the program executes, after initializations the first function called (the entry-point of the application) will be the main.main()
(like in C). The program exits immediately and successfully when main.main
returns.
The code or body in functions is enclosed between braces { }. The first { must be on the same line as ...