A blog about software engineering, software architecture, distributed systems, cloud computing, and technology leadership.
Hi, I'm Vinicius Serpa (or simply Mestre). I'm a staff-level software engineer, currently working as a Systems Architect at Nextpower Inc.
by Vinicius Serpa
Go’s built-in functions (or predeclared functions) are available in any Go program without importing any packages. They are part of the language’s predeclared identifiers. Currently, Go provides 18 built-in functions.
append
cap
clear
close
complex
copy
delete
imag
len
make
max
min
new
panic
print
println
real
recover
Adds elements to a slice.
nums := []int{1, 2}
nums = append(nums, 3, 4)
Returns the length of a string, slice, map, channel, or array.
len(nums)
Returns the capacity of a slice, array, or channel.
cap(nums)
This function is commonly used when learning how slices grow internally.
Creates and initializes slices, maps, and channels.
users := make([]string, 0, 100)
cache := make(map[string]string)
jobs := make(chan Job, 10)
Allocates zeroed memory for a type and returns a pointer to it.
u := new(User)
Equivalent to: u := &User{} in many cases.
Removes a specific key from a map.
delete(users, "carlos")
Removes all elements from a map.
clear(users)
It also works with slices and was introduced in recent Go versions.
Copies elements between slices.
dst := make([]int, len(src))
copy(dst, src)
Closes a channel.
close(ch)
Go provides native support for complex numbers.
Creates a complex number.
c := complex(1, 2)
Returns the real part.
real(c)
Returns the imaginary part.
imag(c)
Stops the normal execution flow of the program.
panic("something went wrong")
Allows a program to recover from a panic when called inside a deferred function.
defer func() {
if r := recover(); r != nil {
fmt.Println(r)
}
}()
print("hello")
println("hello")
These functions exist mainly for quick debugging.
In production code, prefer:
fmt.Print()
fmt.Println()
Since Go 1.21, the following built-in functions have been added:
max(10, 20, 30)
min(10, 20, 30)
A good way to memorize the most frequently used built-in functions is:
These are by far the built-ins you will encounter most often in professional Go projects.
tags: golang - programming-language