Functions are first-class citizens in Go.
func functionName(param1 type1, param2 type2) returnType {
return value
}
func add(a int, b int) int {
return a + b
}
result := add(5, 3) // 8
Go functions can return multiple values:
func divide(a, b int) (int, int) {
return a / b, a % b
}
quotient, remainder := divide(10, 3)
func split(sum int) (x, y int) {
x = sum * 4 / 9
y = sum - x
return // returns x, y
}
Interactive Visualization