Thursday 2 May 2019

Go language: Define map using literal notation


You can define a map in literal notation also.

Syntax
mapName := map[dataType1]dataType2{
         key1 : value1,
         key2 : value2,
         .....
         .....
         .....
         keyN : valueN
}

App.go
package main

import "fmt"

func main() {

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

 fmt.Println("Map with two elements : ", employees)
}

Output
Map with two elements :  map[1:Krishna 2:Ram]



Previous                                                 Next                                                 Home

No comments:

Post a Comment