To print a map in Go, you can iterate over the map using a for range loop to print each key-value pair.
We can print a map in Go by iterating over the map using a for range loop and printing each key-value pair.
For example,
myMap
with some key-value pairs. In this example, the map has string keys and integer values.key
and value
represent each key and value in the map.fmt.Printf
function.package main
import "fmt"
func main() {
// Declare and initialize a map
myMap := map[string]int{
"one": 1,
"two": 2,
"three": 3,
}
// Print the map using a for range loop
for key, value := range myMap {
fmt.Printf("%s: %d\n", key, value)
}
}
one: 1 two: 2 three: 3
We can print a map in Go by iterating over the keys of the map and printing each key-value pair.
For example,
myMap
with some key-value pairs. In this example, the map has string keys and integer values.key
represents each key in the map.fmt.Printf
function.package main
import "fmt"
func main() {
// Declare and initialize a map
myMap := map[string]int{
"one": 1,
"two": 2,
"three": 3,
}
// Print the map by iterating over the keys
for key := range myMap {
fmt.Printf("%s: %d\n", key, myMap[key])
}
}
one: 1 two: 2 three: 3
We can print a map in Go by iterating over the map entries and printing each key-value pair.
For example,
myMap
with some key-value pairs. In this example, the map has string keys and integer values.key
and value
represent each key and value in the map.fmt.Println
function.package main
import "fmt"
func main() {
// Declare and initialize a map
myMap := map[string]int{
"one": 1,
"two": 2,
"three": 3,
}
// Print the map by iterating over the map entries
for key, value := range myMap {
fmt.Println(key, ":", value)
}
}
one : 1 two : 2 three : 3
In this tutorial, we learned How to Print a Map in Go language with well detailed examples.