# golang数据结构之map篇

LittleLee · · 1239 次点击 · · 开始浏览

package main

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

func main() {
var mapInt = make(map[int]int)
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!"

0 回复

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传
package main

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

func main() {
var mapInt = make(map[int]int)
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!"