From b1935a40573446056d8d809a877010376cb0cdc3 Mon Sep 17 00:00:00 2001 From: lbenedar Date: Sat, 21 Feb 2026 16:25:56 +0300 Subject: [PATCH] add ch12_3 --- ch12/3/ch12_3.go | 28 ++++++++++++++++++++++++++++ ch12/3/go.mod | 3 +++ 2 files changed, 31 insertions(+) create mode 100644 ch12/3/ch12_3.go create mode 100644 ch12/3/go.mod diff --git a/ch12/3/ch12_3.go b/ch12/3/ch12_3.go new file mode 100644 index 0000000..54c6ba4 --- /dev/null +++ b/ch12/3/ch12_3.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" + "math" + "sync" +) + +func CreateSqrtMap() map[int]float64 { + numKeys := 100_000 + newMap := make(map[int]float64) + + for i := range numKeys { + newMap[i] = math.Sqrt(float64(i)) + } + return newMap +} + +var initCreateMapSqrt func() map[int]float64 = sync.OnceValue(CreateSqrtMap) + +func main() { + sqrtMap := initCreateMapSqrt() + n := 100_000 / 1_000 + for i := range n { + keyVal := i * 1_000 + fmt.Printf("Value of map by key[%d]: %f\n", keyVal, sqrtMap[keyVal]) + } +} diff --git a/ch12/3/go.mod b/ch12/3/go.mod new file mode 100644 index 0000000..d10e9be --- /dev/null +++ b/ch12/3/go.mod @@ -0,0 +1,3 @@ +module gitea.local.lab/Lbenedar/learning_go/ch12/3 + +go 1.25.0