curena
3/17/2019 - 12:28 AM

Map declaration in Go

Declare a map in Go

//Will declare a map of string keys, int values. 

var timeZone = map[string]int{
    "UTC":  0*60*60,
    "EST": -5*60*60,
    "CST": -6*60*60,
    "MST": -7*60*60,
    "PST": -8*60*60,
}

//You can create a set-like structure using a map by setting the value of each key to bool.

attended := map[string]bool{
  "Ann": true,
  "Bobby": true
}

//This is useful now because you can check for existence in the map
if attended[somePerson] { //Will return false if somePerson isn't in the map
  fmt.Printf("%v was present.\n")
}