Friday, 3 May 2019

Go language: Print all the keys of map


You can print all the keys of a map using for loop.

Example
for key := range employees {
         fmt.Println(key)
}

App.go
package main

import "fmt"

func main() {

 employees := map[int]string{
  1: "Krishna",
  2: "Ram",
  3: "Chamu", //Must have trailing comma
 }

 for key := range employees {
  fmt.Println(key)
 }
}

Output
1
2
3

Previous                                                 Next                                                 Home

No comments:

Post a Comment