Memory Allocation
make
Allocates and initializes an object of type slice, map, or channel.The type to allocate. Must be a slice, map, or channel type.
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.
Returns the initialized object. Unlike
new, returns the same type as the argument, not a pointer.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.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
Pointer to the newly allocated variable.
Slice Operations
append
Appends elements to the end of a slice and returns the updated slice.The slice to append to.
Elements to append. Can also use
... to append another slice.The updated slice. May have a new underlying array if capacity was exceeded.
copy
Copies elements from a source slice to a destination slice.Destination slice.
Source slice. Can also be a string when copying to
[]byte.Number of elements copied (minimum of
len(src) and len(dst)).clear
Clears all elements in a map or slice.The map or slice to clear. If nil,
clear is a no-op.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.The map to delete from.
The key to delete.
Size and Capacity
len
Returns the length of various types.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
The length of v.
cap
Returns the capacity of various types.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
The capacity of v.
Channel Operations
close
Closes a channel.The channel to close. Must be bidirectional or send-only.
Numeric Operations
min
Returns the smallest value from a set of ordered values.First value. Type must implement
cmp.Ordered.Additional values to compare.
The smallest value. Returns NaN if any floating-point argument is NaN.
max
Returns the largest value from a set of ordered values.First value. Type must implement
cmp.Ordered.Additional values to compare.
The largest value. Returns NaN if any floating-point argument is NaN.
Complex Number Operations
complex
Constructs a complex number from real and imaginary parts.Real part (float32 or float64).
Imaginary part (must be same type as real part).
Complex number (complex64 for float32, complex128 for float64).
real
Returns the real part of a complex number.Complex number.
Real part of the complex number.
imag
Returns the imaginary part of a complex number.Complex number.
Imaginary part of the complex number.
Error Handling
panic
Stops normal execution and begins panicking.The panic value. Starting in Go 1.21, passing nil causes a run-time error.
recover
Regains control of a panicking goroutine.The value passed to
panic, or nil if not panicking or if called outside a deferred function.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
Values to print.
println
Formats and writes arguments to standard error with spaces between arguments and a trailing newline.Values to print.