go sync.Once

 

 

 

 

package main

import "fmt"
import "sync"
import "time"


var oc sync.Once
var a int = 5;


func main() {
    
    go func(){
       oc.Do(func(){
           a++
       }) 
    }()
    
    
    go func(){
       oc.Do(func(){
           a++
       }) 
    }()
    
    time.Sleep(time.Second)
    
    fmt.Println(a)
}

 

 

 

posted @ 2022-10-16 00:40  AngDH  阅读(18)  评论(0编辑  收藏  举报