This repository was archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples_test.go
More file actions
66 lines (50 loc) · 1.48 KB
/
Copy pathexamples_test.go
File metadata and controls
66 lines (50 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package alloc_test
import (
"fmt"
"time"
"github.com/wtask-go/alloc"
)
// fictional data model
type model struct {
Value *float64
Modified *time.Time
}
func (m *model) Print() {
if m == nil {
fmt.Println("model: <nil>")
return
}
fmt.Printf("model: {%v, %v}\n", alloc.Value(m.Value), alloc.Value(m.Modified))
}
const defaultValue = 3.14
func Example_use() {
m := &model{
Value: alloc.New(defaultValue), // useful referencing to constant values
}
m.Print()
m.Value = alloc.New(alloc.Value(m.Value) * 2) // inline operations with referenced values
m.Modified = alloc.New(time.Date(2023, 5, 8, 15, 0, 0, 0, time.UTC))
m.Print()
val := alloc.Copy(m.Value)
*val = defaultValue // m.value and val are referenced to different values
fmt.Printf("m.value: %v, val: %v\n", alloc.Value(m.Value), alloc.Value(val))
m.Value = nil
if _, ok := alloc.Deref(m.Value); !ok {
// synthetic case due to you able to directly check m.Value == nil
fmt.Println("update is not required: nil value")
}
// pipelined computation with pointers
if v := alloc.Value[float64](nil); v != 0 {
fmt.Println("division result:", defaultValue/v)
} else {
fmt.Println("invalid division by zero")
}
fmt.Println("subtraction:", defaultValue-alloc.Value[float64](nil))
// Output:
// model: {3.14, 0001-01-01 00:00:00 +0000 UTC}
// model: {6.28, 2023-05-08 15:00:00 +0000 UTC}
// m.value: 6.28, val: 3.14
// update is not required: nil value
// invalid division by zero
// subtraction: 3.14
}