golang数据结构之map篇

LittleLee · · 1918 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

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!"

  


有疑问加站长微信联系(非本文作者)

本文来自:博客园

感谢作者:LittleLee

查看原文:golang数据结构之map篇

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1918 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传