-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfull_example.go
More file actions
100 lines (77 loc) · 1.9 KB
/
full_example.go
File metadata and controls
100 lines (77 loc) · 1.9 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"fmt"
"strings"
"sync"
"github.com/golang-common-packages/linear"
)
func runExamples() {
// Basic usage
basicExample()
// Size checker example
sizeLimitExample()
// Complex data example
complexDataExample()
// Concurrent operations
concurrentExample()
// Error handling
errorHandlingExample()
}
func basicExample() {
fmt.Println("\n=== Basic Example ===")
client := linear.New(1024, false)
client.Push("1", "a")
client.Push("2", "b")
val, _ := client.Pop()
fmt.Println("Popped:", val) // b
val, _ = client.Take()
fmt.Println("Taken:", val) // a
}
func sizeLimitExample() {
fmt.Println("\n=== Size Limit Example ===")
client := linear.New(100, true) // Enable size checker
for i := 0; i < 5; i++ {
client.Push(fmt.Sprint(i), strings.Repeat("x", 20))
}
fmt.Println("Items count:", client.GetNumberOfKeys()) // Will be limited by size
}
func complexDataExample() {
fmt.Println("\n=== Complex Data Example ===")
client := linear.New(1024, false)
type Person struct {
Name string
Age int
}
client.Push("person1", Person{"Alice", 30})
client.Push("person2", Person{"Bob", 25})
val, _ := client.Get("person1")
fmt.Println("Got person:", val)
}
func concurrentExample() {
fmt.Println("\n=== Concurrent Example ===")
client := linear.New(10000, true)
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
key := fmt.Sprint(i)
client.Push(key, "value"+key)
}(i)
}
wg.Wait()
fmt.Println("Concurrent items count:", client.GetNumberOfKeys())
}
func errorHandlingExample() {
fmt.Println("\n=== Error Handling Example ===")
client := linear.New(100, false)
// Empty key
err := client.Push("", "value")
fmt.Println("Empty key error:", err)
// Nil value
err = client.Push("key", nil)
fmt.Println("Nil value error:", err)
// Pop from empty
_, err = client.Pop()
fmt.Println("Pop from empty error:", err)
}