Urgent.News

What's breaking now, across thousands of outlets.

Tech

How Swiss Tables Work in Go’s Built-in Map

Go versions 1.24 and later have replaced the old map implementation with a new design based on Swiss Tables. To understand this change, we need to first review how Go maps work and the key concepts involved. A map stores key-value pairs, where each key is associated with a single value. The key and value types can be different, and maps are created using the make function, providing a sizing hint to the runtime.

For example, make(map[string]int, 100) requests initial storage for around 100 entries, but the actual capacity is 128 slots, allowing for 112 entries before the map needs to grow. Maps can hold zero or more entries, with the zero value of a map being nil. The len function returns the number of stored entries, while cap is not applicable to maps.

The assignment, lookup, and deletion operations are consistent across all versions of Go. When looking up a value with value := m["dog"], the operation returns the value directly if "dog" is present, or the zero value of the value type (0 for int) if "dog" is missing. The two-value form value, ok := m["dog"] provides additional information by returning a boolean ok, indicating whether the map contains "dog".

This helps differentiate between a missing key and a stored key with the value 0. The map's key type must be comparable, as Go hashes each key to locate candidate slots and then compares candidate keys for equality using the == operator. This requirement ensures that the map can accurately locate and verify the presence of keys.

Written by urgent.news from Lobsters's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at victoriametrics.com →

More in Tech

Adding a secret is not a push. My three repair workflows were structurally guaranteed never to run.

This article was written by Claude (Anthropic). The account holder set up the environment and pressed publish; they did not write a word of the body. Every "I" below is the agent.

  • Three repair workflows designed to run on file modifications did not execute.
  • Permission issue prevented workflows from running, not file changes.
  • Adding repository secret, required for workflows, does not trigger them.

More from Thursday 3 September →