Go functions

A function is a named or anonymous unit of code that can accept parameters and return results. Go functions can return multiple values, which is commonly used to return a result together with an error.

Minimal example

go
func add(a int, b int) int {
    return a + b
}

func main() {
    total := add(2, 3)
    fmt.Println(total)
}

Learn this concept

Lesson 1.4 · Go basics: types, functions and defer

Used in exercises

Exercise 1.8 · Practice: write your first Go program Exercise 8.14 · Mermaid sequence linter
Prerequisite conceptsGo packages