go实现安全并发map读写


本文摘自网络,作者,侵删。

参考

E:\svn\golang\pkg\mod\github.com\i!google-ink\gopay

代码实现

package gopay

import (
    "encoding/json"
    "encoding/xml"
    "errors"
    "io"
    "sort"
    "strings"
    "sync"
)

type BodyMap map[string]interface{}

var mu sync.RWMutex

// 设置参数
func (bm BodyMap) Set(key string, value interface{}) {
    mu.Lock()
    bm[key] = value
    mu.Unlock()
}

// 获取参数
func (bm BodyMap) Get(key string) string {
    if bm == nil {
        return NULL
    }
    mu.RLock()
    defer mu.RUnlock()
    value, ok := bm[key]
    if !ok {
        return NULL
    }
    v, ok := value.(string)
    if !ok {
        return convertToString(value)
    }
    return v
}

// 删除参数
func (bm BodyMap) Remove(key string) {
    mu.Lock()
    delete(bm, key)
    mu.Unlock()
}



本文来自:简书

感谢作者:我是不会赢的

查看原文:go实现安全并发map读写

相关阅读 >>

Golang- Go语言学习笔记之定义变量

Golang使用protobuf中的oneof

Go循环队列的实现

Golang log如何设计

搭建vscode Golang开发环境

聊聊dubbo-Go-proxy的accesslogfilter

Go语言反射的使用

快看!Go-carbon 1.2.2 版本发布了!新增了时间设置和时间差比较功能

Golang 程序结构

堆排序

更多相关阅读请进入《Go》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...