Arrays, Slices, Maps & Strings·Q40·Medium
A slice is a small struct of three words, {ptr *T, len int, cap int} . It holds a pointer to a backing array, the number of elements you can see ( len ), and how many…
Arrays, Slices, Maps & Strings·Q41·Hard
func add(s []int) { s = append(s, 4) s[0] = 100 } func main() { s := make([]int, 3, 4) add(s) fmt.Println(s, len(s), cap(s)) fmt.Println(s[:4]) t := []int{1, 2, 3} //…
Arrays, Slices, Maps & Strings·Q42·Hard
When len + n > cap , append calls runtime.growslice . Since Go 1.20 the rule ( nextslicecap ) is: If the needed length is more than twice the old cap, use the needed…
Arrays, Slices, Maps & Strings·Q43·Hard
x := make([]int, 0, 5) x = append(x, 1, 2, 3) y := append(x, 4) z := append(x, 5) fmt.Println(x, y, z) Output: [1 2 3] [1 2 3 5] [1 2 3 5] x has len 3 and cap 5. Both…
Arrays, Slices, Maps & Strings·Q44·Medium
a[low:high:max] makes a slice with len = high-low and cap = max-low . The constraint is 0 <= low <= high <= max <= cap(a) . A plain a[low:high] keeps all the capacity up…
Arrays, Slices, Maps & Strings·Q45·Medium
var s []int is nil : ptr is nil, len and cap are 0. s := []int{} or make([]int, 0) is empty but non-nil : ptr points at a zero-size allocation (often runtime.zerobase ).…
Arrays, Slices, Maps & Strings·Q46·Medium
copy(dst, src) copies min(len(dst), len(src)) elements and returns that count. It uses len, not cap . So copying into make([]T, 0, n) copies nothing, which is a common…
Arrays, Slices, Maps & Strings·Q47·Medium
An array's length is part of its type ( [4]int and [5]int are different types). An array is a value : assigning it, passing it, or ranging over it copies every element.…
Arrays, Slices, Maps & Strings·Q48·Hard
arr := [3]int{1, 2, 3} for i, v := range arr { arr[2] = 100 if i == 2 { fmt.Println("array:", v) } } sl := []int{1, 2, 3} for i, v := range sl { sl[2] = 100 if i == 2 {…
Arrays, Slices, Maps & Strings·Q49·Hard
A subslice keeps the entire backing array reachable, not just the part you can see. If you read a 10 MB file and keep data[:16] in a long-lived cache, all 10 MB stays…
Arrays, Slices, Maps & Strings·Q50·Medium
There are three common patterns: Order-preserving delete: O(n), using append or copy. Unordered delete: O(1), swap with the last element. In-place filter: O(n), no…
Arrays, Slices, Maps & Strings·Q51·Medium
Slices, maps, and functions are not comparable . You can compare them only to nil . The language designers left out == for slices on purpose, because it is ambiguous:…
Arrays, Slices, Maps & Strings·Q52·Medium
make([][]int, n) creates only the outer slice of n nil rows. Writing grid[0][0] panics until you allocate each row. There are two approaches. // 1) Row-per-allocation:…
Arrays, Slices, Maps & Strings·Q53·Hard
If a function is declared as func Filter[E any](s []E) []E , then passing a named slice type like type IDs []int returns plain []int . The caller loses the named type…
Arrays, Slices, Maps & Strings·Q54·Medium
Go 1.21 added slices and maps to the standard library. Go 1.23 added iterator-returning functions based on iter.Seq / iter.Seq2 and range-over-func. s := []int{3, 1, 4,…
Arrays, Slices, Maps & Strings·Q55·Hard
The classic runtime map ( runtime.hmap ) is a hash table with 2^B buckets : Each bucket ( bmap ) holds 8 entries . It starts with tophash [8]uint8 , the top 8 bits of…
Arrays, Slices, Maps & Strings·Q56·Hard
Go 1.24 replaced the bucket-chaining map with an implementation based on Swiss Tables (Google's Abseil design), in internal/runtime/maps . The design has several parts:…
Arrays, Slices, Maps & Strings·Q57·Medium
The spec says iteration order is unspecified . The runtime goes further and deliberately randomizes it: each range starts at a random position (in the classic map, a…
Arrays, Slices, Maps & Strings·Q58·Medium
Yes, within one goroutine. The spec defines the behaviour: Deleting an entry that has not been reached yet means it will not be produced. Deleting the current key is…
Arrays, Slices, Maps & Strings·Q59·Hard
Built-in maps are not safe for concurrent use if at least one goroutine writes. Concurrent reads alone are fine. The runtime has a cheap best-effort check (a hashWriting…
Arrays, Slices, Maps & Strings·Q60·Hard
The sync.Map docs name two cases where it wins: The entry for a key is written once and read many times , as in caches that only grow. Many goroutines read, write, and…
Arrays, Slices, Maps & Strings·Q61·Medium
Map elements are not addressable . m["k"] gives you a copy of the value, and assigning to a field of a temporary copy would do nothing. So the compiler rejects it:…
Arrays, Slices, Maps & Strings·Q62·Medium
Because the runtime moves map entries . When a map grows (classic evacuation, or a Swiss-table split), entries are rehashed and copied to new memory. A pointer from…
Arrays, Slices, Maps & Strings·Q63·Hard
Keys must be comparable : booleans, numbers, strings, pointers, channels, interfaces, and arrays or structs made only of comparable types. Slices, maps, and functions…
Arrays, Slices, Maps & Strings·Q64·Hard
No. delete marks slots as empty (or as tombstones in Swiss tables), but the bucket or group memory is never given back. A map that once held 1M entries keeps its…
Arrays, Slices, Maps & Strings·Q65·Medium
clear (Go 1.21) has different semantics depending on the type: Map: deletes every entry, so len becomes 0. It is the only way to remove NaN keys, and it is faster than…
Arrays, Slices, Maps & Strings·Q66·Medium
A map variable is a pointer to a runtime header . Passing it copies the pointer, so inserts and deletes inside the callee are visible to the caller, without needing *map…
Arrays, Slices, Maps & Strings·Q67·Hard
A string is a two-word header, {ptr *byte, len int} , pointing at read-only bytes. Immutability means strings can be shared freely: substrings and assignments copy no…
Arrays, Slices, Maps & Strings·Q68·Medium
s := "héllo" fmt.Println(len(s), utf8.RuneCountInString(s)) fmt.Println(s[1], string(s[1])) for i, r := range s { fmt.Printf("%d:%c ", i, r) } fmt.Println()…
Arrays, Slices, Maps & Strings·Q69·Medium
Because strings are immutable, s += x in a loop allocates a new string and copies everything built so far each time, which is O(n²) . The options compare like this: +…
Arrays, Slices, Maps & Strings·Q70·Hard
Go 1.20 added the supported primitives unsafe.String , unsafe.StringData and unsafe.SliceData . They replace the old reflect.StringHeader / SliceHeader hacks, which are…
Arrays, Slices, Maps & Strings·Q71·Medium
Go has no built-in set type. The idiom is map[T]struct{} . The empty struct takes zero bytes , so values cost nothing, and the type makes the intent clear. map[T]bool…
Arrays, Slices, Maps & Strings·Q72·Hard
var ptrs []*int for _, v := range []int{1, 2, 3} { ptrs = append(ptrs, &v) } var funcs []func() for i := 0; i < 3; i++ { funcs = append(funcs, func() { fmt.Print(i, " ")…
Arrays, Slices, Maps & Strings·Q73·Hard
base := make([]int, 0, 4) m := map[string][]int{} m["a"] = append(base, 1) m["b"] = append(base, 2) fmt.Println(m["a"], m["b"]) m["a"] = append(m["a"], 10)…
Arrays, Slices, Maps & Strings·Q74·Hard
func main() { var p *[3]int fmt.Println(len(p)) for i := range p { fmt.Print(i, " ") } fmt.Println() for i := range 3 { // Go 1.22 range-over-int fmt.Print(i, " ") }…
Arrays, Slices, Maps & Strings·Q75·Hard
Since Go 1.23 a for range loop can take a function of type func(yield func(K, V) bool) (that is, iter.Seq2[K, V] ), func(yield func(V) bool) ( iter.Seq[V] ) or…
Arrays, Slices, Maps & Strings·Q76·Hard
unique.Make[T comparable](v T) Handle[T] canonicalizes (interns) values. All calls with equal values return handles that point to the same single copy. Comparing two…