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 可以开发 web 吗?

关于 Golang 字符串 格式化

模块三 Go语言实战与应用-测试的基本规则和流程(上)

分享一些为phper准备的Go入门知识

windows下如何玩转火热的Go-zero

Go并发编程实战学习(一)

【必看】标准的 Go 项目布局

Golang可以写web吗?

Golang sqlx捕捉错误

Golang 写个希尔排序

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




打赏

取消

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

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

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

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

评论

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