Исходники и презентации
This commit is contained in:
31
lessons/functions/lazy_evaluation/main.go
Normal file
31
lessons/functions/lazy_evaluation/main.go
Normal file
@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type LazyMap func() map[string]string
|
||||
|
||||
func Make(ctr func() map[string]string) LazyMap {
|
||||
var initialized bool
|
||||
var data map[string]string
|
||||
return func() map[string]string {
|
||||
if !initialized {
|
||||
data = ctr()
|
||||
initialized = true
|
||||
ctr = nil // for GC
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
data := Make(func() map[string]string {
|
||||
return make(map[string]string)
|
||||
})
|
||||
|
||||
fmt.Println(data())
|
||||
data()["key"] = "value"
|
||||
fmt.Println(data())
|
||||
}
|
||||
Reference in New Issue
Block a user