✌ Go provides another important data type map that maps unique keys to values. A key is an object that you use to retrieve a value at a later date. Given a key and a value, you can store the value in a Map object. After the value is stored, you can retrieve it by using its key.
A Go map type looks like this:
map[KeyType]ValueType
Declaration and initialization:
1) m := make(map[string]int)
2) m := map[string]int{}
3) var m map[string]int{}
Map to get key of value :
1). i := m["route"]
2). i, ok := m["route"]
3). for key, value := range m {
fmt.Println("Key:", key, "Value:", value)
}
The built-in len function returns on the number of items in a map:
n := len(m)
The built-in delete function removes an entry from the map:
delete(m, "rouzte")
GO Program :
package main
import ( "fmt" )
func main() {
var m = make(map[string]int)
m["m"] = 24
m["a"] = 31
m["ma"] = 21
for key, value := range m {
fmt.Println("Key:", key, "Value:", value)
}
}
Comments
Post a Comment