Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A function type in Go denotes the set of all functions that share the exact same parameter and result signatures. Because Go treats functions as first-class citizens, function types allow the compiler to type-check functions as values, enabling them to be assigned to variables, passed as arguments, or returned from other functions.

Syntax and Declaration

A custom function type is declared using the type keyword, followed by the type name, the func keyword, and the signature.
type TypeName func(ParameterList) ReturnList
You can also use function types anonymously directly in variable declarations or function signatures without defining a named type:
var operation func(int, int) int

Type Identity and Signature Matching

For a function to be assignable to a specific function type, its signature must match the type definition exactly. Go enforces strict rules for function type identity:
  1. Parameter Matching: The number, order, and types of all parameters must be identical.
  2. Return Matching: The number, order, and types of all return values must be identical.
  3. Name Independence: The names of the parameters and the names of the return values (if named returns are used) are entirely ignored by the compiler when determining type identity.
  4. Variadic Distinction: A variadic parameter (e.g., ...int) is distinct from a slice parameter (e.g., []int). A function with a variadic parameter does not match a function type expecting a slice, and vice versa.
type BinaryOperation func(int, int) int

// MATCHES: Exact signature match.
func add(a int, b int) int { 
    return a + b 
}

// MATCHES: Parameter and return names are ignored by the type system.
func multiply(x, y int) (result int) { 
    return x * y 
}

// DOES NOT MATCH: Returns an additional error type.
func divide(a, b int) (int, error) { 
    return a / b, nil 
}

// DOES NOT MATCH: Parameter types are float64 instead of int.
func subtract(a, b float64) float64 { 
    return a - b 
}

The Zero Value

The zero value of an uninitialized function type is nil. Attempting to invoke a nil function value results in a runtime panic.
var op BinaryOperation // op is currently nil

if op != nil {
    op(5, 3)
} else {
    // Invoking op(5, 3) here would cause a panic: 
    // panic: runtime error: invalid memory address or nil pointer dereference
}

Instantiation via Anonymous Functions

Function types are frequently instantiated using anonymous functions (function literals). The compiler infers the type of the anonymous function and verifies it against the target variable’s function type.
var subtract BinaryOperation = func(a, b int) int {
    return a - b
}
If the anonymous function captures variables from its surrounding lexical scope, it forms a closure. The function type itself remains unchanged regardless of whether the underlying function value is a standard function or a closure; the type system only concerns itself with the signature.
Master Go with Deep Grasping Methodology!Learn More