Functions
Functions are central to Go. They allow you to organize code into reusable blocks that can accept parameters and return values. Go supports various function patterns including multiple return values and variadic parameters.
Key Points
- Functions are defined with the
funckeyword - Parameters of the same type can share a type declaration
- Functions can return multiple values (common for returning value and error)
- Named return values are automatically initialized to zero values
- Functions are first-class citizens and can be passed around like values
- Go does not support function overloading
- All parameters are passed by value (copies), use pointers for pass-by-reference behavior
- Unused parameters can be replaced with
_(blank identifier)
Basic Function Declaration
Functions are declared using the func keyword, followed by the name, parameters, and return type:
package main
import "fmt"
// Function that takes two ints and returns their sum
func plus(a int, b int) int {
return a + b
}
// When parameters are of the same type, you can omit type for all but last
func plusPlus(a, b, c int) int {
return a + b + c
}
func main() {
res := plus(1, 2)
fmt.Println("1+2 =", res)
res = plusPlus(1, 2, 3)
fmt.Println("1+2+3 =", res)
}1+2 = 3
1+2+3 = 6Functions as Values
Functions are first-class values in Go and can be assigned to variables, passed as arguments, and returned from other functions:
package main
import "fmt"
func compute(fn func(int, int) int) int {
return fn(3, 4)
}
func main() {
// Assign function to variable
add := func(a, b int) int {
return a + b
}
multiply := func(a, b int) int {
return a * b
}
fmt.Println("Add:", compute(add))
fmt.Println("Multiply:", compute(multiply))
}Add: 7
Multiply: 12Function Declaration Patterns
| Pattern | Example | Description |
|---|---|---|
func name(param type) returnType | func add(x int) int | Single parameter, single return |
func name(p1, p2 type) | func add(x, y int) | Multiple params of same type |
func name(p1 t1, p2 t2) | func fn(x int, y string) | Multiple params, different types |
func name() (r1, r2 type) | func vals() (int, int) | Multiple return values |
func name() (x, y int) | func split() (x, y int) | Named return values |
func name(vals ...type) | func sum(nums ...int) | Variadic function (accepts variable args) |
func() { } | fn := func() { fmt.Println() } | Anonymous function (function literal) |
Function Characteristics
| Feature | Description | Example |
|---|---|---|
| First-class values | Functions can be assigned to variables | fn := func() { } |
| Higher-order functions | Functions can accept/return functions | func process(fn func(int) int) |
| Closures | Functions can capture variables from outer scope | func() int { return x } |
| Multiple returns | Functions can return multiple values | func vals() (int, string) |
| Named returns | Return values can be named and initialized | func split() (x, y int) |
| Variadic parameters | Functions can accept variable arguments | func sum(nums ...int) |
| Deferred execution | Functions can be deferred until surrounding function returns | defer cleanup() |