【Golang】使用 - sync.Once

Posted by 西维蜀黍 on 2021-06-28, Last Modified on 2021-09-21
package main

import (
    "fmt"
    "sync"
)

var doOnce sync.Once

func main() {
    DoSomething()
    DoSomething()
}

func DoSomething() {
    doOnce.Do(func() {
        fmt.Println("Run once - first time, loading...")
    })
    fmt.Println("Run this every time")
}

Reference


TOC