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