GO并发环境下使用map(sync.Map)

package main

import (
	"fmt"
	"sync"
)

func main() {
	//无需初始化,直接声明即可
	var person sync.Map

	//将键值保存到sync.Map
	person.Store(2, "John")
	person.Store(5, "Mike")
	person.Store(6, "Kell")

	//取值
	fmt.Println(person.Load(2))

	//删除键值对
	person.Delete(2)

	//遍历
	person.Range(func(k, v interface{}) bool {
		fmt.Println("Value:", k, v)
		//return true 代表继续遍历
		//return false 代表结束遍历
		return true
	})

	fmt.Println("End...")
}
发布了131 篇原创文章 · 获赞 81 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/wei242425445/article/details/88416843
今日推荐