Go Gopher How to Go

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 func keyword
  • 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)
}
Output:
1+2 = 3
1+2+3 = 6

Functions 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))
}
Output:
Add: 7
Multiply: 12

Function Declaration Patterns

PatternExampleDescription
func name(param type) returnTypefunc add(x int) intSingle 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

FeatureDescriptionExample
First-class valuesFunctions can be assigned to variablesfn := func() { }
Higher-order functionsFunctions can accept/return functionsfunc process(fn func(int) int)
ClosuresFunctions can capture variables from outer scopefunc() int { return x }
Multiple returnsFunctions can return multiple valuesfunc vals() (int, string)
Named returnsReturn values can be named and initializedfunc split() (x, y int)
Variadic parametersFunctions can accept variable argumentsfunc sum(nums ...int)
Deferred executionFunctions can be deferred until surrounding function returnsdefer cleanup()