Skip to main content
Go provides a set of predeclared functions that are available in all packages without import. These functions provide essential operations for working with slices, maps, channels, and managing program flow.

Memory Allocation

make

Allocates and initializes an object of type slice, map, or channel.
t
Type
required
The type to allocate. Must be a slice, map, or channel type.
size
IntegerType
Size specification. Meaning varies by type:
  • Slice: Length (required). Capacity defaults to length.
  • Map: Initial capacity hint (optional).
  • Channel: Buffer capacity (optional). Zero creates unbuffered channel.
result
Type
required
Returns the initialized object. Unlike new, returns the same type as the argument, not a pointer.
// Create slice with length 5, capacity 5
s := make([]int, 5)

// Create slice with length 0, capacity 10
s := make([]int, 0, 10)
The make function only works with slices, maps, and channels. For other types, use new.

new

Allocates memory for a new variable and returns a pointer to it.
TypeOrExpr
TypeOrExpr
required
Either a type or an expression:
  • Type T: Allocates variable of type T initialized to zero value
  • Expression x: Allocates variable of x’s type initialized to x’s value
result
*Type
required
Pointer to the newly allocated variable.
// Allocate int initialized to 0
p := new(int)
fmt.Println(*p) // 0

// Allocate struct initialized to zero values
type Point struct { X, Y int }
p := new(Point)
fmt.Println(*p) // {0 0}

Slice Operations

append

Appends elements to the end of a slice and returns the updated slice.
slice
[]Type
required
The slice to append to.
elems
...Type
required
Elements to append. Can also use ... to append another slice.
result
[]Type
required
The updated slice. May have a new underlying array if capacity was exceeded.
slice := []int{1, 2, 3}
slice = append(slice, 4, 5)
fmt.Println(slice) // [1 2 3 4 5]
Always assign the result of append back to a variable. If the slice’s capacity is exceeded, append allocates a new array and the old slice will be unchanged.

copy

Copies elements from a source slice to a destination slice.
dst
[]Type
required
Destination slice.
src
[]Type
required
Source slice. Can also be a string when copying to []byte.
result
int
required
Number of elements copied (minimum of len(src) and len(dst)).
src := []int{1, 2, 3, 4, 5}
dst := make([]int, 3)
n := copy(dst, src)
fmt.Println(dst) // [1 2 3]
fmt.Println(n)   // 3
The source and destination slices may overlap. copy handles overlapping regions correctly.

clear

Clears all elements in a map or slice.
t
~[]Type | ~map[Type]Type1
required
The map or slice to clear. If nil, clear is a no-op.
m := map[string]int{"a": 1, "b": 2}
clear(m)
fmt.Println(len(m)) // 0
For maps, clear deletes all entries. For slices, clear sets all elements up to the length to their zero value but does not change the length or capacity.

Map Operations

delete

Deletes an element from a map.
m
map[Type]Type1
required
The map to delete from.
key
Type
required
The key to delete.
m := map[string]int{"a": 1, "b": 2, "c": 3}
delete(m, "b")
fmt.Println(m) // map[a:1 c:3]

// Delete non-existent key is a no-op
delete(m, "z")

// Delete from nil map is a no-op
var nilMap map[string]int
delete(nilMap, "key") // No panic

Size and Capacity

len

Returns the length of various types.
v
Type
required
Value to measure. Supported types:
  • Array: number of elements
  • Pointer to array: number of elements in *v (even if v is nil)
  • Slice or map: number of elements; zero if v is nil
  • String: number of bytes
  • Channel: number of queued elements; zero if v is nil
result
int
required
The length of v.
// Array
arr := [3]int{1, 2, 3}
fmt.Println(len(arr)) // 3

// Slice
slice := []int{1, 2, 3, 4, 5}
fmt.Println(len(slice)) // 5

// Map
m := map[string]int{"a": 1, "b": 2}
fmt.Println(len(m)) // 2

// String
s := "hello"
fmt.Println(len(s)) // 5

// Channel
ch := make(chan int, 3)
ch <- 1
ch <- 2
fmt.Println(len(ch)) // 2
For some arguments like string literals or simple array expressions, len can be evaluated as a compile-time constant.

cap

Returns the capacity of various types.
v
Type
required
Value to measure. Supported types:
  • Array: number of elements (same as len)
  • Pointer to array: number of elements (same as len)
  • Slice: maximum length when resliced; zero if v is nil
  • Channel: buffer capacity; zero if v is nil
result
int
required
The capacity of v.
// Slice
slice := make([]int, 5, 10)
fmt.Println(len(slice)) // 5
fmt.Println(cap(slice)) // 10

// Array
arr := [5]int{}
fmt.Println(cap(arr)) // 5

// Channel
ch := make(chan int, 100)
fmt.Println(cap(ch)) // 100

Channel Operations

close

Closes a channel.
c
chan<- Type
required
The channel to close. Must be bidirectional or send-only.
ch := make(chan int)

go func() {
    ch <- 1
    ch <- 2
    ch <- 3
    close(ch) // Signal no more values
}()

for v := range ch {
    fmt.Println(v) // 1, 2, 3
}

// Receiving from closed channel returns zero value
v, ok := <-ch
fmt.Println(v, ok) // 0 false
Only the sender should close a channel, never the receiver. Sending to a closed channel causes a panic.

Numeric Operations

min

Returns the smallest value from a set of ordered values.
x
T
required
First value. Type must implement cmp.Ordered.
y
...T
Additional values to compare.
result
T
required
The smallest value. Returns NaN if any floating-point argument is NaN.
fmt.Println(min(1, 2, 3))           // 1
fmt.Println(min(3.14, 2.71, 1.41))  // 1.41
fmt.Println(min("apple", "banana")) // "apple"

max

Returns the largest value from a set of ordered values.
x
T
required
First value. Type must implement cmp.Ordered.
y
...T
Additional values to compare.
result
T
required
The largest value. Returns NaN if any floating-point argument is NaN.
fmt.Println(max(1, 2, 3))           // 3
fmt.Println(max(3.14, 2.71, 1.41))  // 3.14
fmt.Println(max("apple", "banana")) // "banana"

Complex Number Operations

complex

Constructs a complex number from real and imaginary parts.
r
FloatType
required
Real part (float32 or float64).
i
FloatType
required
Imaginary part (must be same type as real part).
result
ComplexType
required
Complex number (complex64 for float32, complex128 for float64).
c := complex(3.0, 4.0)
fmt.Println(c) // (3+4i)

real

Returns the real part of a complex number.
c
ComplexType
required
Complex number.
result
FloatType
required
Real part of the complex number.
c := complex(3.0, 4.0)
fmt.Println(real(c)) // 3.0

imag

Returns the imaginary part of a complex number.
c
ComplexType
required
Complex number.
result
FloatType
required
Imaginary part of the complex number.
c := complex(3.0, 4.0)
fmt.Println(imag(c)) // 4.0

Error Handling

panic

Stops normal execution and begins panicking.
v
any
required
The panic value. Starting in Go 1.21, passing nil causes a run-time error.
func validateAge(age int) {
    if age < 0 {
        panic("age cannot be negative")
    }
}

func main() {
    defer func() {
        if r := recover(); r != nil {
            fmt.Println("Recovered:", r)
        }
    }()
    
    validateAge(-5) // Panics
    fmt.Println("This won't execute")
}
Panicking should be reserved for truly exceptional situations. For normal error handling, use error values.

recover

Regains control of a panicking goroutine.
result
any
required
The value passed to panic, or nil if not panicking or if called outside a deferred function.
func safeDivide(a, b int) (result int, err error) {
    defer func() {
        if r := recover(); r != nil {
            err = fmt.Errorf("panic: %v", r)
        }
    }()
    
    return a / b, nil
}

result, err := safeDivide(10, 0)
if err != nil {
    fmt.Println(err) // panic: runtime error: integer divide by zero
}
recover only works when called directly from a deferred function. Calling it from functions called by the deferred function will not stop the panic.

Debugging

print

Formats and writes arguments to standard error.
args
...Type
Values to print.
print("Debug: ", 42, "\n")
print is for bootstrapping and debugging only. It is not guaranteed to remain in the language. Use fmt.Print for production code.

println

Formats and writes arguments to standard error with spaces between arguments and a trailing newline.
args
...Type
Values to print.
println("Debug:", 42)
println is for bootstrapping and debugging only. It is not guaranteed to remain in the language. Use fmt.Println for production code.

Build docs developers (and LLMs) love