Menu Close

Go – How to iterate over map in go

In this tutorial, We are going to learn about how to iterate over map in go. We can do it by using for loop and range in go.

Syntax:

for key, value := range map_name

Example:

map_name = 

{"Sunday": 0,
 "Monday": 1,
 "Tuesday": 2
}

For loop with range:

Sunday: 0
Monday: 1
Tuesday: 2

Example with code:

package main

import (

  "fmt"
)

func main() {

  m := map[string]int {"Sunday": 0, "Monday": 1, "Tuesday": 2}

  for key, val := range m {

    fmt.Println(key, ":  ", val)

  }
}

Output:

Sunday : 0

Monday : 1

Tuesday : 2

To learn more about golang, You can refer given below link:

https://www.techieindoor.com/go-lang-tutorial/

References:

 https://golang.org/doc/
https://golang.org/pkg/
https://golang.org/pkg/fmt/
https://golang.org/pkg/fmt/#Println
Posted in golang, Miscellaneous

Leave a Reply

Your email address will not be published.

Contact Us