-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
111 lines (97 loc) · 2.02 KB
/
example_test.go
File metadata and controls
111 lines (97 loc) · 2.02 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
101
102
103
104
105
106
107
108
109
110
111
package iprange_test
import (
"fmt"
"log"
"math/big"
"net/netip"
"github.com/iiiceoo/iprange"
)
func ExampleParse() {
v4Ranges, err := iprange.Parse("172.18.0.1", "172.18.0.0/24")
if err != nil {
log.Fatalf("error parsing IP ranges: %v", err)
}
v6Ranges, err := iprange.Parse("fd00::1-a", "fd00::1-fd00::1:a")
if err != nil {
log.Fatalf("error parsing IP ranges: %v", err)
}
fmt.Println(v4Ranges)
fmt.Println(v6Ranges)
// Output:
// [172.18.0.1 172.18.0.0/24]
// [fd00::1-fd00::a fd00::1-fd00::1:a]
}
func ExampleIPRanges_Contains() {
ranges, err := iprange.Parse("172.18.0.0/24")
if err != nil {
log.Fatalf("error parsing IP ranges: %v", err)
}
fmt.Println(ranges.Contains(netip.MustParseAddr("172.18.0.1")))
fmt.Println(ranges.Contains(netip.MustParseAddr("172.19.0.1")))
fmt.Println(ranges.Contains(netip.MustParseAddr("fd00::1")))
// Output:
// true
// false
// false
}
func ExampleIPRanges_Union() {
ranges1, _ := iprange.Parse("172.18.0.20-30", "172.18.0.1-25")
ranges2, _ := iprange.Parse("172.18.0.5-25")
fmt.Println(ranges1.Union(ranges2))
fmt.Println(ranges1)
// Output:
// 172.18.0.1-172.18.0.30
// [172.18.0.20-172.18.0.30 172.18.0.1-172.18.0.25]
}
func ExampleIPRanges_IPIterator() {
ranges, _ := iprange.Parse("172.18.0.1-3")
iter := ranges.IPIterator()
for {
addr := iter.Next()
if !addr.IsValid() {
break
}
fmt.Println(addr)
}
iter.Reset()
for {
addr := iter.NextN(big.NewInt(2))
if !addr.IsValid() {
break
}
fmt.Println(addr)
}
// Output:
// 172.18.0.1
// 172.18.0.2
// 172.18.0.3
// 172.18.0.2
}
func ExampleIPRanges_BlockIterator() {
ranges, err := iprange.Parse("172.18.0.0-4")
if err != nil {
log.Fatalf("error parsing IP ranges: %v", err)
}
iter := ranges.BlockIterator(big.NewInt(2))
for {
ip := iter.Next()
if ip == nil {
break
}
fmt.Println(ip)
}
iter.Reset()
n := big.NewInt(3)
for {
ip := iter.NextN(n)
if ip == nil {
break
}
fmt.Println(ip)
}
// Output:
// 172.18.0.0/31
// 172.18.0.2/31
// 172.18.0.4
// 172.18.0.4
}