go maps code example
Example 1: create map golang
//map in go is a built in type implementaiton of has table
//create a empty map
myMap := make(map[string]string)
//insert key-value pair in map
myMap["key"] = "value"
//read from map
value, ok := myMap["key"]
//delete from map
delete(myMap, "key")
Example 2: go add to map
m := make(map[string]int)
m["numberOne"] = 1
m["numberTwo"] = 2
Example 3: go maps
type Vertex struct {
Lat, Long float64
}
var m map[string]Vertex
func main() {
m = make(map[string]Vertex)
m["Bell Labs"] = Vertex{
40.68433, -74.39967,
}
fmt.Println(m["Bell Labs"])
}
Example 4: go maps
var m map[string]int
m = make(map[string]int)
m["key"] = 42
fmt.Println(m["key"])
delete(m, "key")
elem, ok := m["key"] // test if key "key" is present and retrieve it, if so
// map literal
var m = map[string]Vertex{
"Bell Labs": {40.68433, -74.39967},
"Google": {37.42202, -122.08408},
}
// iterate over map content
for key, value := range m {
}
Example 5: what is the use of map in golang
Search Results
Featured snippet from the web
In Go language, a map is a powerful, ingenious, and a versatile data structure. Golang Maps is a collection of unordered pairs of key-value. It is widely used because it provides fast lookups and values that can retrieve, update or delete with the help of keys. It is a reference to a hash table.