go 函数并发编程中提供了三种锁类型:互斥锁(mutex)、读写锁(rwmutex)、一次性锁(once)。互斥锁保证独占访问,读写锁允许并发读取和单一写入,一次性锁确保代码块仅执行一次。这些锁用于协调对共享资源的访问,防止数据竞争。实践中,互斥锁可确保并发 web 服务中的数据一致性,阻止并发请求同时修改共享数据结构。

Go 函数并发编程中的锁类型和用法
在 Go 函数并发编程中,锁是一种同步机制,用于协调对共享资源的访问,防止数据竞争。Go 中提供了多种类型的锁,每种锁都有不同的特性和适用场景。
互斥锁
互斥锁(Mutex)是一个基础的锁,只能同时允许一个 goroutine 获取资源。它保证了对共享资源的独占访问。
import (
"sync"
"fmt"
)
var (
mu sync.Mutex
counter int
)
func main() {
for i := 0; i <h3>读写锁</h3><p>读写锁(RWMutex)允许多个 goroutine 并发读取共享资源,但只有一个 goroutine 可以同时写入资源。</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill8661" title="Golang Uber Dig"><img
src="https://img.php.cn/upload/skill/000/000/081/179215391842073.jpg" alt="Golang Uber Dig" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/skill8661" title="Golang Uber Dig" class="overflowclass">Golang Uber Dig</a>
<p class="overflowclass">使用 uber-go/dig 在 Golang 中实现依赖注入——基于反射容器,提供 Provide/Invoke、dig.In/dig.Out 参数与结果对象,支持命名依赖等</p>
</div>
<a rel="nofollow" href="/xiazai/skill8661" title="Golang Uber Dig" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div><pre class="brush:go;toolbar:false;">import (
"sync"
"fmt"
)
var (
rwmu sync.RWMutex
shared []int
)
func main() {
// 多个 goroutine 并发读取共享切片
for i := 0; i <h3>Once</h3><p>Once 是一个一次性锁,用于确保特定代码块只执行一次。</p><pre class="brush:go;toolbar:false;">import (
"sync"
"fmt"
)
var (
initOnce sync.Once
inited = false
)
func initialize() {
inited = true
fmt.Println("Initialized")
}
func main() {
initOnce.Do(initialize)
if inited {
fmt.Println("Already initialized")
} else {
fmt.Println("Not initialized")
}
}实战案例:确保并发 web 服务中的数据一致性
假设有一个 web 服务,其中多并发请求需要操作同一个共享数据结构。为了确保数据的一致性,可以使用互斥锁来保护数据结构,阻止并发请求同时对其进行修改。
import (
"sync"
"net/http"
)
var (
mu sync.Mutex
clients map[string]*http.Client
)
func main() {
http.HandleFunc("/addClient", func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
clients[r.FormValue("name")] = &http.Client{}
mu.Unlock()
})
}在本例中,使用互斥锁 mu 保护对 clients 地图的并发访问,确保在同一时间只有一个请求可以添加或修改客户信息,从而避免了数据竞争。
golang免费学习笔记(深入):立即使用
在学习笔记中,你将探索golang的核心概念和高级技巧!










