Using Map in Go


A simple list of multiple usages of map in Go:

package main

import "fmt"

func main() {
    // Like in Ruby, values in maps passed to functions are original values,
    // modifying the values will modify the original.

    // Declaration:
    // var my_map map[string]string, or:
    my_map := make(map[string]string)

    // Using "=" to assign or update a value
    my_map["John"] = "Great"
    my_map["Bob"] = "Good"
    my_map["Mary"] = "Awesome"

    // Quick declaration:
    // The first "string" is the type of a key, and the second is for the value
    another_map := map[string]string{
        "John": "Great",
        "Bob":  "Good",
        "Mary": "Awesome",
    }

    // Deletion
    delete(my_map, "Mary")

    // With if statement, the second returned value is a boolean value,
    // indicating if the key exists in this map.
    if value, exists := my_map["John"]; exists {
        fmt.Println(value + " exists")
    }

    for i, c := range my_map {
        fmt.Println(i)
        fmt.Println(c)
    }
}