mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-20 07:24:41 +02:00
A nicer implementation of sync.Once
Doesnt require storing the result of the function in a dedicated global variable with a dedicated getter function
This commit is contained in:
24
tools/utils/once_test.go
Normal file
24
tools/utils/once_test.go
Normal file
@@ -0,0 +1,24 @@
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user