This repository has been archived by the owner on May 9, 2020. It is now read-only.
forked from bakanis/uuid
-
Notifications
You must be signed in to change notification settings - Fork 13
/
examples_test.go
111 lines (84 loc) · 2.6 KB
/
examples_test.go
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 uuid_test
import (
"fmt"
"github.com/twinj/uuid"
"github.com/twinj/uuid/savers"
"net/url"
"time"
)
func Example() {
saver := new(savers.FileSystemSaver)
saver.Report = true
saver.Duration = time.Second * 3
// Run before any v1 or v2 UUIDs to ensure the savers takes
uuid.RegisterSaver(saver)
up, _ := uuid.Parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
fmt.Printf("version %d variant %x: %s\n", up.Version(), up.Variant(), up)
uuid.New(up.Bytes())
u1 := uuid.NewV1()
fmt.Printf("version %d variant %x: %s\n", u1.Version(), u1.Variant(), u1)
u4 := uuid.NewV4()
fmt.Printf("version %d variant %x: %s\n", u4.Version(), u4.Variant(), u4)
u3 := uuid.NewV3(u1, u4)
url, _ := url.Parse("www.example.com")
u5 := uuid.NewV5(uuid.NameSpaceURL, url)
if uuid.Equal(u1, u3) {
fmt.Println("Will never happen")
}
if uuid.Compare(uuid.NameSpaceDNS, uuid.NameSpaceDNS) == 0 {
fmt.Println("They are equal")
}
// Default Format is Canonical
fmt.Println(uuid.Formatter(u5, uuid.FormatCanonicalCurly))
uuid.SwitchFormat(uuid.FormatCanonicalBracket)
}
func ExampleNewV1() {
// Must run before using V1 or V2
uuid.Init()
u1 := uuid.NewV1()
fmt.Printf("version %d variant %d: %d\n", u1.Version(), u1.Variant(), u1)
}
func ExampleNewV2() {
// Must run before using V1 or V2
uuid.Init()
u2 := uuid.NewV2(uuid.DomainUser)
fmt.Printf("version %d variant %d: %d\n", u2.Version(), u2.Variant(), u2)
}
func ExampleNewV3() {
u, _ := uuid.Parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
u3 := uuid.NewV3(u, uuid.Name("test"))
fmt.Printf("version %d variant %x: %s\n", u3.Version(), u3.Variant(), u3)
}
func ExampleNewV4() {
u4 := uuid.NewV4()
fmt.Printf("version %d variant %x: %s\n", u4.Version(), u4.Variant(), u4)
}
func ExampleNewV5() {
u5 := uuid.NewV5(uuid.NameSpaceURL, uuid.Name("test"))
fmt.Printf("version %d variant %x: %s\n", u5.Version(), u5.Variant(), u5)
}
func ExampleParse() {
u, err := uuid.Parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
if err != nil {
fmt.Println("error:", err)
}
fmt.Println(u)
}
func ExampleRegisterSaver() {
saver := new(savers.FileSystemSaver)
saver.Report = true
saver.Duration = 3 * time.Second
// Run before any v1 or v2 UUIDs to ensure the savers takes
uuid.RegisterSaver(saver)
u1 := uuid.NewV1()
fmt.Printf("version %d variant %x: %s\n", u1.Version(), u1.Variant(), u1)
}
func ExampleFormatter() {
u4 := uuid.NewV4()
fmt.Println(uuid.Formatter(u4, uuid.FormatCanonicalCurly))
}
func ExampleSwitchFormat() {
uuid.SwitchFormat(uuid.FormatCanonicalBracket)
u4 := uuid.NewV4()
fmt.Printf("version %d variant %x: %s\n", u4.Version(), u4.Variant(), u4)
}