1package nosync 2 3// Once is an object that will perform exactly one action. 4type Once struct { 5 doing bool 6 done bool 7} 8 9// Do calls the function f if and only if Do is being called for the 10// first time for this instance of Once. In other words, given 11// var once Once 12// if once.Do(f) is called multiple times, only the first call will invoke f, 13// even if f has a different value in each invocation. A new instance of 14// Once is required for each function to execute. 15// 16// Do is intended for initialization that must be run exactly once. Since f 17// is niladic, it may be necessary to use a function literal to capture the 18// arguments to a function to be invoked by Do: 19// config.once.Do(func() { config.init(filename) }) 20// 21// If f causes Do to be called, it will panic. 22// 23// If f panics, Do considers it to have returned; future calls of Do return 24// without calling f. 25// 26func (o *Once) Do(f func()) { 27 if o.done { 28 return 29 } 30 if o.doing { 31 panic("nosync: Do called within f") 32 } 33 o.doing = true 34 defer func() { 35 o.doing = false 36 o.done = true 37 }() 38 f() 39} 40