Functions in Go

From the Go cheat sheet ยท Functions ยท verified Jul 2026

Functions

Function declaration and usage

go
// Basic function
func add(x, y int) int {
    return x + y
}

// Multiple returns
func swap(x, y string) (string, string) {
    return y, x
}

// Named returns
func split(sum int) (x, y int) {
    x = sum * 4 / 9
    y = sum - x
    return  // Naked return
}
๐Ÿ“ฆ Multiple return values for errors
๐Ÿ”„ defer for cleanup (LIFO order)
๐Ÿ“ Variadic functions with ...
โšก Functions are first-class values

More Go tasks

Back to the full Go cheat sheet