Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Go

Idiom #281 Use a Point as a map key

You have a Point with integer coordinates x and y. Create a map m with key type Point (or equivalent) and value type string. Insert "Hello" at position (42, 5).

m := map[Point]string{}
p := Point{x: 42, y: 5}
m[p] = "Hello"

Types comparable with == can be used as map keys.
m[new Point(x: 42, y: 5)] = "Hello";

New implementation...
programming-idioms.org