Friday 25 September 2020

golang5 map

 package main

import "fmt"

func main() {
var mp map[string]int = map[string]int{
"apple":  5,
"pear":   6,
"orange": 9,
}
fmt.Println(mp)

mp1 := make(map[string]string)
fmt.Println(mp1)

mp1["dog"] = "pet"
mp1["cat"] = "pet"
fmt.Println(mp1)

delete(mp1, "cat")
fmt.Println(mp1)

val, exist := mp1["dog"]
fmt.Println(val, exist)

fmt.Println(len(mp1))
}

//cmd

C:\Users\bob\golang1>go run tutorial.go
map[apple:5 orange:9 pear:6]
map[]
map[cat:pet dog:pet]
map[dog:pet]
pet true
1

reference:

No comments:

Post a Comment