Collections

21 articles
How Go Maps Work Internally (Hash Table Implementation) Go maps use a hash table with buckets and concurrent rehashing for fast, non-blocking lookups and resizing. How Go Slices Work Internally (Slice Header) A Go slice is a descriptor with a pointer, length, and capacity that references a portion of an underlying array. How to Check If a Key Exists in a Map in Go Check if a key exists in a Go map using the comma-ok idiom to safely retrieve the value and a boolean status. How to Check If a Slice Contains a Value in Go Use slices.Contains to check if a value exists in a Go slice. How to Convert a Slice to a Map in Go Convert a Go slice to a map by iterating through the slice and assigning key-value pairs to a new map instance. How to Create and Use Maps in Go Create Go maps with make or literals and access values using bracket notation with keys. How to Create a Slice of Structs in Go Create a slice of structs by declaring it with `make` or an empty literal, then append initialized struct instances to it. How to Delete a Key from a Map in Go Use the built-in `delete()` function with the map and the key you want to remove. How to Filter a Slice in Go Filter a Go slice using slices.DeleteFunc to remove items in place or slices.Filter to create a new slice with matching elements. How to Flatten a Slice of Slices in Go Flatten a slice of slices in Go using slices.Concat or a loop with append. How to Get a Sub-Slice in Go (Slice of a Slice) You get a sub-slice in Go by using slice slicing syntax `slice[low:high]`, which creates a new slice header pointing to the same underlying array as the original. How to Iterate Over a Map in Go (And Why Order Is Random) Iterate over a Go map using a range loop, but expect random order as Go intentionally shuffles traversal to prevent reliance on sequence. How to Iterate Over a Map in Go with range Iterate over a Go map using the range keyword to access keys and values in a single loop. How to Merge Two Maps in Go Merge two Go maps using maps.Copy in Go 1.21+ or a manual loop for earlier versions. How to Merge Two Slices in Go You can merge two slices in Go by creating a new slice with a capacity equal to the sum of both lengths, then using the built-in `append` function to copy elements from the first slice followed by the second. How to Sort a Map by Key or Value in Go Go maps are unordered; extract keys or values into a slice and use slices.Sort to order them. How to Sort a Slice in Go (sort.Slice and slices.Sort) Use slices.Sort for basic types and sort.Slice for custom types or logic to order Go slices. How to Use a Map as a Set in Go Use a map with boolean values to simulate a set in Go for tracking unique items efficiently. How to Use a Map with Struct Keys in Go Use structs as Go map keys only if all fields are comparable types, or implement custom hashing for complex cases. How to Use the make Function for Slices and Maps in Go Use the make built-in function to initialize slices with length/capacity and maps with initial capacity in Go. What Is a Nil Slice vs an Empty Slice in Go A nil slice has no underlying array, whereas an empty slice has an underlying array but zero length.