add ch12_3
This commit is contained in:
28
ch12/3/ch12_3.go
Normal file
28
ch12/3/ch12_3.go
Normal file
@@ -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])
|
||||
}
|
||||
}
|
||||
3
ch12/3/go.mod
Normal file
3
ch12/3/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module gitea.local.lab/Lbenedar/learning_go/ch12/3
|
||||
|
||||
go 1.25.0
|
||||
Reference in New Issue
Block a user