kitty/tools/utils/once_test.go
Kovid Goyal 6f4d89045a
A nicer implementation of sync.Once
Doesnt require storing the result of the function in a dedicated global
variable with a dedicated getter function
2023-02-26 08:01:02 +05:30

25 lines
348 B
Go

// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
package utils
import (
"fmt"
"testing"
)
var _ = fmt.Print
func TestOnce(t *testing.T) {
num := 0
var G = (&Once[string]{Run: func() string {
num++
return fmt.Sprintf("%d", num)
}}).Get
G()
G()
G()
if num != 1 {
t.Fatalf("num unexpectedly: %d", num)
}
}