Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To expand a little on answers already given:</p> <p>A Go map is a typed hash map data structure. A map's type signature is of the form <code>map[keyType]valueType</code> where <code>keyType</code> and <code>valueType</code> are the types of the keys and values respectively.</p> <p>To initialize a map, you must use the <code>make</code> function:</p> <pre><code>m := make(map[string]int) </code></pre> <p>An uninitialized map is equal to <code>nil</code>, and if read from or written a panic will occur at runtime.</p> <p>The syntax for storing values is much the same as doing so with arrays or slices:</p> <pre><code>m["Alice"] = 21 m["Bob"] = 17 </code></pre> <p>Similarly, retrieving values from a map is done like so:</p> <pre><code>a := m["Alice"] b := m["Bob"] </code></pre> <p>You can use the <code>range</code> keyword to iterate over a map with a <code>for</code> loop:</p> <pre><code>for k, v := range m { fmt.Println(k, v) } </code></pre> <p>This code will print:</p> <pre><code>Alice 21 Bob 17 </code></pre> <p>Retrieving a value for a key that is not in the map will return the value type's zero value:</p> <pre><code>c := m["Charlie"] // c == 0 </code></pre> <p>By reading multiple values from a map, you can test for a key's presence. The second value will be a boolean indicating the key's presence:</p> <pre><code>a, ok := m["Alice"] // a == 21, ok == true c, ok := m["Charlie"] // c == 0, ok == false </code></pre> <p>To remove a key/value entry from a map, you flip it around and assign <code>false</code> as the second value:</p> <pre><code>m["Bob"] = 0, false b, ok := m["Bob"] // b == 0, ok == false </code></pre> <p>You can store arbitrary types in a map by using the empty interface type <code>interface{}</code>:</p> <pre><code>n := make(map[string]interface{}) n["One"] = 1 n["Two"] = "Two" </code></pre> <p>The only proviso is that when retrieving those values you must perform a type assertion to use them in their original form:</p> <pre><code>a := n["One"].(int) b := n["Two"].(string) </code></pre> <p>You can use a type switch to determine the types of the values you're pulling out, and deal with them appropriately:</p> <pre><code>for k, v := range n { switch u := v.(type) { case int: fmt.Printf("Key %q is an int with the value %v.\n", k, u) case string: fmt.Printf("Key %q is a string with the value %q.\n", k, u) } } </code></pre> <p>Inside each of those <code>case</code> blocks, <code>u</code> will be of the type specified in the <code>case</code> statement; no explicit type assertion is necessary.</p> <p>This code will print:</p> <pre><code>Key "One" is an int with the value 1. Key "Two" is a string with the value "Two". </code></pre> <p>The key can be of any type for which the equality operator is defined, such as integers, floats, strings, and pointers. Interface types can also be used, as long as the underlying type supports equality. (Structs, arrays and slices cannot be used as map keys, because equality is not defined on those types.)</p> <p>For example, the map <code>o</code> can take keys of any of the above types:</p> <pre><code>o := make(map[interface{}]int) o[1] = 1 o["Two"] = 2 </code></pre> <p>And that's maps in a nutshell.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload