Table of Contents
Maps
A map
is an unordered collection of key-value pairs, where each key is unique. Maps
in Go are called hashes
or dictionaries
in other programming languages.
Declaring a map
You can define a map
using the following syntax.
① make(map[keyType]valueType)
② map[keyType]valueType { key1: value1, key2: value2, ......., keyX: valueX}
① make(map[keyType]valueType)
You can initialize a map using the built-in make()
function. The make()
function returns a map
of the given type, initialized and ready for use. Keys
and corresponding values
can be added to a map like the code below.
package main
import "fmt"
func main() {
var map1 = make(map[int]string)
fmt.Println(map1) //=> map[]
//Add keys and values to 'map1'
map1[1] = "Go"
map1[2] = "Ruby"
fmt.Println(map1) //=> map[1:Go 2:Ruby]
}
② map[keyType]valueType { key1: value1, key2: value2, …, keyX: valueX}
By using the map literal
, you can initialize a map
with some initial data.
package main
import "fmt"
func main() {
var map2 = map[int]string { 1: "Go", 2:"Ruby"}
fmt.Println(map2) //=> map[1:Go 2:Ruby]
}
Nil maps
If you declare a map
with the map literal
without initial data, it would generate a nil-map
. Needless to say, a nil-map
does not contain any data. Moreover, any attempt to add any data to a nil-map
causes a runtime error.
package main
import "fmt"
func main() {
var nilMap map[int]string
if nilMap == nil {
fmt.Println("nil") //=> nil
}
nilMap[1] = "GO" //=> assignment to entry in nil map
}
Modyfing a map
You can add data to a map
and modify the data of a map
like the folloing code.
package main
import "fmt"
func main(){
var mapEx = make(map[int]string)
//add keys and values to a map
mapEx[1] = "GO"
mapEx[2] = "Ruby"
fmt.Println(mapEx) //=> map[1:GO 2:Ruby]
//modify the data of a map
mapEx[1] = "Python"
mapEx[2] = "Java"
fmt.Println(mapEx) //=> map[1:Python 2:Java]
}
Retrieving values from a map
You can retrieve the value assigned to a key in a map using the syntax map[key]
.
package main
import "fmt"
func main(){
var mapEx = make(map[int]string)
mapEx[1] = "GO"
mapEx[2] = "Ruby"
fmt.Println(mapEx[1]) //=> GO
Checking the existence of a key in a map
When you retrieve the value assigned to a given key using the syntax map[key]
, it returns an additional boolean value as well. It returns true
if the key exists in the map and returns false
if it does not exist in the map.
package main
import "fmt"
func main(){
var mapEx = make(map[int]string)
mapEx[1] = "GO"
mapEx[2] = "Ruby"
//Trying to retrieve a key that exists in mapEx.
lang, ok := mapEx[1]
//Trying to retrieve a key that does not exist in mapEx.
lang2, ok2 := mapEx[4]
fmt.Println(lang, ok) //=> GO true
fmt.Println(lang2, ok2) //=> false
}
If you just want to check for the existence of a key without retrieving the value associated with that key, then you can use an _ (underscore)
in place of the first value.
package main
import "fmt"
func main(){
var mapEx = make(map[int]string)
mapEx[1] = "GO"
mapEx[2] = "Ruby"
_, ok := mapEx[1]
_, ok2 := mapEx[4]
fmt.Println(ok) //=> true
fmt.Println(ok2) //=> false
}
Deleting a key from a map
You can delete a key from a map using the built-in delete(map, key)
function. The delete()
function does not do anything if the key does not exist in the map.
package main
import "fmt"
func main(){
var mapEx = make(map[int]string)
mapEx[1] = "GO"
mapEx[2] = "Ruby"
delete(mapEx, 1)
fmt.Println(mapEx) //=> map[2:Ruby]
}
Range
The range
keyword is used in for loop
to iterate over items of an array, slice, channel or map. When you are ranging over an array
or a slice
, it returns the index and the element at that index. When you are ranging over a map, it returns the key and the key-value.
Array
package main
import "fmt"
var arryEx = [3]string { "Go", "Ruby", "Python" }
func main(){
for index, value := range arryEx {
fmt.Println(index, value)
//=> 0 Go
//=> 1 Ruby
//=> 2 Python
}
}
Slice
package main
import "fmt"
var sliceEx = []string { "Go", "Ruby", "Python" }
for index, ele := range sliceEx {
fmt.Println(index, ele)
//=> 0 Go
//=> 1 Ruby
//=> 2 Python
}
}
Map
package main
import "fmt"
var mapEx = map[string]string{ "Name":"Sam", "Gender":"Male" }
for key, value := range mapEx {
fmt.Println(key, value)
//=> Name Sam
//=> Gender Male
}
}
Skipping the index, key or value
You can skip the index
or value
by assigning to _
. If you only want the index, drop the value
entirely.
Index Only
package main
import "fmt"
var arryEx = [3]string { "Go", "Ruby", "Python" }
func main(){
for index, _ := range arryEx {
fmt.Println(index)
//=> 0
//=> 1
//=> 2
}
}
Value Only
package main
import "fmt"
var arryEx = [3]string { "Go", "Ruby", "Python" }
func main(){
for _, value := range arryEx {
fmt.Println(value)
//=> Go
//=> Ruby
//=> Python
}
}