Go Cheatsheet
50 essential methods for coding interviews
Showing 50 of 50 methods
| Method | Syntax | Description | Time | Priority |
|---|---|---|---|---|
make (slice) | make([]T, length, capacity) | Create a slice with specified length and optional capacity | O(n) | essential |
append | append(slice, elements...) | Append elements to a slice, returns new slice | O(1) amortized, O(n) when growing | essential |
copy | copy(dst, src) | Copy elements from src to dst slice, returns count copied | O(n) | essential |
len | len(slice) | Return the number of elements in a slice, array, map, string, or channel | O(1) | essential |
slice expression | slice[low:high] or slice[low:high:max] | Create a sub-slice from index low to high-1 | O(1) | essential |
delete element (slice) | append(s[:i], s[i+1:]...) | Remove element at index i from slice | O(n) | essential |
reverse slice | for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { s[i], s[j] = s[j], s[i] } | Reverse a slice in-place using two-pointer technique | O(n) | essential |
make (map) | make(map[K]V) or make(map[K]V, hint) | Create a map with optional size hint | O(1) | essential |
map literal | map[K]V{k1: v1, k2: v2} | Create and initialize a map with literal syntax | O(n) | essential |
map access | value, ok := m[key] | Access map value with existence check | O(1) average | essential |
delete (map) | delete(m, key) | Remove a key-value pair from a map | O(1) average | essential |
range (map) | for key, value := range m { } | Iterate over map keys and values | O(n) | essential |
strings.Contains | strings.Contains(s, substr) | Check if string contains substring | O(n*m) | essential |
strings.Split | strings.Split(s, sep) | Split string by separator into slice | O(n) | essential |
strings.Join | strings.Join(slice, sep) | Join string slice with separator | O(n) | essential |
strings.Index | strings.Index(s, substr) | Find index of first occurrence of substring (-1 if not found) | O(n*m) | essential |
strings.ToLower/ToUpper | strings.ToLower(s) / strings.ToUpper(s) | Convert string to lowercase or uppercase | O(n) | essential |
string to []byte | []byte(s) | Convert string to byte slice for mutation | O(n) | essential |
string to []rune | []rune(s) | Convert string to rune slice for Unicode-safe operations | O(n) | essential |
strings.Builder | var sb strings.Builder; sb.WriteString(s) | Efficiently build strings with minimal allocations | O(1) amortized per write | essential |
sort.Ints | sort.Ints(slice) | Sort int slice in ascending order in-place | O(n log n) | essential |
sort.Strings | sort.Strings(slice) | Sort string slice in ascending order in-place | O(n log n * k) where k is string length | essential |
sort.Slice | sort.Slice(slice, less func(i, j int) bool) | Sort slice with custom comparison function | O(n log n) | essential |
sort.Search | sort.Search(n, f func(i int) bool) | Binary search for smallest index where f(i) is true | O(log n) | essential |
strconv.Itoa | strconv.Itoa(i) | Convert int to string | O(log n) | essential |
strconv.Atoi | strconv.Atoi(s) | Convert string to int (returns error if invalid) | O(n) | essential |
fmt.Sprintf | fmt.Sprintf(format, args...) | Format values into a string | O(n) | essential |
math.Max/Min (float64) | math.Max(a, b) / math.Min(a, b) | Return max/min of two float64 values | O(1) | essential |
for range (slice) | for i, v := range slice { } | Iterate over slice with index and value | O(n) | essential |
for range (string) | for i, r := range s { } | Iterate over string runes (Unicode code points) | O(n) | essential |
error handling | if err != nil { return err } | Standard Go error handling pattern | O(1) | essential |
defer | defer func() | Schedule function call to run when surrounding function returns | O(1) | essential |
two-pointer technique | for l, r := 0, len(s)-1; l < r; l, r = l+1, r-1 { } | Common pattern for array/string problems | O(n) | essential |
sliding window | for r := 0; r < len(s); r++ { /* expand */ for condition { l++ /* shrink */ } } | Pattern for subarray/substring problems | O(n) | essential |
cap | cap(slice) | Return the capacity of a slice or array | O(1) | common |
insert element (slice) | append(s[:i], append([]T{x}, s[i:]...)...) | Insert element x at index i | O(n) | common |
strings.Replace | strings.Replace(s, old, new, n) | Replace first n occurrences (n=-1 for all) | O(n) | common |
strings.TrimSpace | strings.TrimSpace(s) | Remove leading and trailing whitespace | O(n) | common |
strings.HasPrefix/HasSuffix | strings.HasPrefix(s, prefix) / strings.HasSuffix(s, suffix) | Check if string starts or ends with given substring | O(k) where k is prefix/suffix length | common |
sort.SearchInts | sort.SearchInts(slice, x) | Binary search for x in sorted int slice | O(log n) | common |
strconv.ParseInt | strconv.ParseInt(s, base, bitSize) | Parse string to int64 with base and bit size control | O(n) | common |
math.Abs | math.Abs(x) | Return absolute value of float64 | O(1) | common |
math.Sqrt | math.Sqrt(x) | Return square root of x | O(1) | common |
math.Pow | math.Pow(x, y) | Return x raised to power y | O(1) | common |
go (goroutine) | go func() { }() | Launch a new goroutine | O(1) | common |
make (channel) | make(chan T) or make(chan T, buffer) | Create unbuffered or buffered channel | O(1) | common |
sync.WaitGroup | var wg sync.WaitGroup; wg.Add(n); wg.Done(); wg.Wait() | Wait for a collection of goroutines to finish | O(1) | common |
sync.Mutex | var mu sync.Mutex; mu.Lock(); mu.Unlock() | Mutual exclusion lock for protecting shared data | O(1) | common |
container/heap | heap.Push(h, x); heap.Pop(h) | Min-heap operations (implement heap.Interface) | O(log n) push/pop | common |
container/list | list.New(); l.PushBack(x); l.PushFront(x) | Doubly linked list | O(1) insert/delete | useful |