Go🔧Core Languagebeginner
Functions
Go · Core Language · Run with go run
Explanation
Functions are first-class values in Go. They can return multiple values — idiomatic Go uses this for `(result, error)` pairs instead of exceptions. Functions can be assigned to variables, passed as arguments, and defined as closures capturing their surrounding scope.
Real-World Scenario
A database query function returns `(User, error)`. The caller handles the error inline: `user, err := db.Find(id); if err != nil { return err }`. No try/catch, no stack unwinding — explicit and predictable.
Quick Snippet
snippet.go
// Multiple return values
func divide(a, b float64) (float64, error) {
if b == 0 {
return 0, fmt.Errorf("division by zero")
}
return a / b, nil
}
// Named return values (document what each value means)
func minMax(nums []int) (min, max int) {
min, max = nums[0], nums[0]
for _, n := range nums[1:] {
if n < min { min = n }
if n > max { max = n }
}
return // naked return
}
// Function as value
double := func(x int) int { return x * 2 }Full Example
functions.go
// Compile: go run functions.go
// Output:
// 5 / 2 = 2.5
// min=1 max=9
// doubled: 8
package main
import "fmt"
func divide(a, b float64) (float64, error) {
if b == 0 {
return 0, fmt.Errorf("division by zero")
}
return a / b, nil
}
func minMax(nums []int) (min, max int) {
min, max = nums[0], nums[0]
for _, n := range nums[1:] {
if n < min {
min = n
}
if n > max {
max = n
}
}
return
}
func main() {
result, err := divide(5, 2)
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Printf("5 / 2 = %.1f\n", result)
min, max := minMax([]int{3, 1, 9, 4, 7})
fmt.Printf("min=%d max=%d\n", min, max)
double := func(x int) int { return x * 2 }
fmt.Println("doubled:", double(4))
}
Run command
go run functions.go