Skip to content

Commit 624b51d

Browse files
committed
Use sync.OnceValue for lazy regexp initialization
Using sync.OnceValue instead of the local implementation brings some benefits in allocations, panic handling and mutex use / concurrent access after initialization. Signed-off-by: Kimmo Lehto <[email protected]>
1 parent a947377 commit 624b51d

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

regexes.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,9 @@ const (
8080
)
8181

8282
func lazyRegexCompile(str string) func() *regexp.Regexp {
83-
var regex *regexp.Regexp
84-
var once sync.Once
85-
return func() *regexp.Regexp {
86-
once.Do(func() {
87-
regex = regexp.MustCompile(str)
88-
})
89-
return regex
90-
}
83+
return sync.OnceValue(func() *regexp.Regexp {
84+
return regexp.MustCompile(str)
85+
})
9186
}
9287

9388
var (

0 commit comments

Comments
 (0)