golang数据结构之map篇

package main

import (
	"github.com/sanity-io/litter"
)

func main() {
	var mapInt = make(map[int]int)
	// add
	for i := 1; i < 10; i++ {
		mapInt[i] = i
	}
	litter.Dump(mapInt)
	// update
	mapInt[3] = 0
	litter.Dump(mapInt)
	// del
	delete(mapInt, 3)
	litter.Dump(mapInt)
	// del when iterator
	for k, v := range mapInt {
		if v % 2 == 0 {
			delete(mapInt, k)
		}
	}
	litter.Dump(mapInt)
	// query
	v, ok := mapInt[9]
	if ok {
		litter.Dump(v, "is exist!")
	}
}

output
map[int]int{
  1: 1,
  2: 2,
  3: 3,
  4: 4,
  5: 5,
  6: 6,
  7: 7,
  8: 8,
  9: 9,
}
map[int]int{
  1: 1,
  2: 2,
  3: 0,
  4: 4,
  5: 5,
  6: 6,
  7: 7,
  8: 8,
  9: 9,
}
map[int]int{
  1: 1,
  2: 2,
  4: 4,
  5: 5,
  6: 6,
  7: 7,
  8: 8,
  9: 9,
}
map[int]int{
  1: 1,
  5: 5,
  7: 7,
  9: 9,
}
9 "is exist!"

  

猜你喜欢

转载自www.cnblogs.com/LittleLee/p/9387775.html