December 2018
Intermediate to advanced
500 pages
12h 19m
English
As in the C/C++ world, there are code blocks called functions. They are defined by the func keyword. They have a name, some parameters, the main body of code, and optionally, a list of results. The following code block defines a function to calculate the area of a circle:
func area(radius int) float64 { var pi float64 = 3.14 return pi*radius*radius}
It accepts a single variable, radius, of the int type, and returns a single float64 value. Within the function, a variable called pi of the float64 type is declared.
Functions in Go can return multiple values. A common case is to return the function result and an error value as a pair, as seen in the following example:
func GetX() (x X, err error)myX, err := GetX()if err ...
Read now
Unlock full access