对于从全局的角度只需要运行一次的代码,比如全局初化操始作,go语言提供了一个Once类型来保证全局的唯一性操作。
typeOnce
Once is an object that will perform exactly one action.
type Once struct {
// contains filtered or unexported fields
}
func (*Once)Do
func (o *Once) Do(f func())
Do calls the function f if and only if Do is being called for the first time for this instance of Once. In other words,given
var once Onceif once.Do(f) is called multiple times,only the first call will invoke f,even if f has a different value in each invocation. A new instance of Once is required for each function to execute.
大体意思是说,一个Once对象在全局范围内只会执行一个操作。
e.g. 单线程环境下演示Once的唯一性
package main
import (
"fmt"
"sync"
)
func f1() {
fmt.Println("This is f1 function")
}
func f2() {
fmt.Println("This is f2 function")
}
func f3() {
fmt.Println("This is f3 function")
}
func main() {
var once sync.Once
once.Do(f1)
once.Do(f2)
once.Do(f3)
}运行:
C:/go/bin/go.exe run test.go [E:/project/go/lx/src]
This is f1 function
从上面的运行结果可以看出,只有f1函数被调用了。一旦一个Once对象的Do方法被调用,那么接下来对该Once对象Do方法的调用都将不会执行。
e.g.多线程环境下演示Once的唯一性
package main
import (
"fmt"
"sync"
"time"
)
var a string
var once sync.Once
func setup() {
fmt.Println("setup begins.")
a = "hello"
for i := 1; i <= 10; i++ {
time.Sleep(1e9)
fmt.Print(".")
}
fmt.Println("\nsetup ends.")
}
func print(wg *sync.WaitGroup) {
once.Do(setup)
fmt.Println(a)
wg.Done()
}
func main() {
var wg sync.WaitGroup
wg.Add(2)
go print(&wg)
go print(&wg)
wg.Wait()
}运行:
C:/go/bin/go.exe run test2.go [E:/project/go/lx/src]
setup begins.
..........
setup ends.
hello
hello
这里需要说明一点:在首次调用once.Do()方法时,其内部会加锁,阻塞其他goroutine对此Do方法的调用,直至全局唯一性操作调用结束,才会释放内部的锁。
原文链接:https://www.f2er.com/go/188447.html