@@ -36,6 +36,11 @@ u, err := uuid.FromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
3636// Parse a UUID from a byte slice
3737u , err := uuid.FromBytes ([]byte {0x6b , 0xa7 , 0xb8 , 0x10 , 0x9d , 0xad , 0x11 , 0xd1 , 0x80 , 0xb4 , 0x00 , 0xc0 , 0x4f , 0xd4 , 0x30 , 0xc8 })
3838
39+ // Format a UUID
40+ asHash := u.Format (uuid.FormatHash )
41+ asBase58 := u.Format (uuid.FormatBase58 )
42+ asCanonical := u.Format (uuid.FormatCanonical )
43+
3944// Scan a SQL UUID
4045var u uuid.UUID
4146err := db.QueryRow (" SELECT id FROM users WHERE email = $1" , email).Scan (&u)
@@ -51,23 +56,27 @@ Changing the default format will affect how UUIDs are marshaled to strings from
5156
5257``` go
5358import (
54- " encoding/json"
55- " github.com/flexstack/uuid"
59+ " encoding/json"
60+ " fmt"
61+
62+ " github.com/flexstack/uuid"
5663)
5764
58- uuid.DefaultFormat = uuid.FormatBase58
65+ func main () {
66+ uuid.DefaultFormat = uuid.FormatBase58
5967
60- u := uuid.FromStringOrNil (" ffffffff-ffff-ffff-ffff-ffffffffffff" )
68+ u := uuid.FromStringOrNil (" ffffffff-ffff-ffff-ffff-ffffffffffff" )
6169
62- // Marshal to base58
63- m := map [string ]uuid.UUID {" id" : u}
64- b , err := json.Marshal (m)
65- fmt.Println (string (b)) // {"id": "YcVfxkQb6JRzqk5kF2tNLv"}
70+ // Marshal to base58
71+ m := map [string ]uuid.UUID {" id" : u}
72+ b , _ := json.Marshal (m)
73+ fmt.Println (string (b)) // {"id": "YcVfxkQb6JRzqk5kF2tNLv"}
74+ }
6675```
6776
6877## Credit
6978
70- This package is a fork of [ github.com/google /uuid] ( https://github.com/gofrs/uuid ) with the following changes:
79+ This package is a fork of [ github.com/gofrs /uuid] ( https://github.com/gofrs/uuid ) with the following changes:
7180
7281- 2x improvement to ` FromString ` , ` UnmarshalText ` , and ` UnmarshalJSON ` performance
7382- Adds base58 encoding.
@@ -76,3 +85,34 @@ This package is a fork of [github.com/google/uuid](https://github.com/gofrs/uuid
7685- Fixes issue with [ TimestampFromV7] ( https://github.com/gofrs/uuid/issues/128 ) not being spec compliant.
7786- Removed v1, v3, v5 UUIDs.
7887- Removed support for braced and URN string formats.
88+
89+ ## Benchmarks
90+
91+ MacBook Air (15-inch, M2, 2023) Apple M2, 24GB RAM, MacOS 14.4.1
92+
93+ ### Format()
94+ ```
95+ Format(FormatCanonical) 44625793 26.54 ns/op 48 B/op 1 allocs/op
96+ Format(FormatHash) 44022964 26.85 ns/op 32 B/op 1 allocs/op
97+ Format(FormatBase58) 5350190 224.0 ns/op 24 B/op 1 allocs/op
98+ ```
99+
100+ ### FromString()
101+ ```
102+ FromString(FormatCanonical) 70893008 16.88 ns/op 0 B/op 0 allocs/op
103+ FromString(FormatBase58) 16760137 71.77 ns/op 0 B/op 0 allocs/op
104+ ```
105+
106+ ### NewVx()
107+ ```
108+ NewV4() 2961621 401.6 ns/op 16 B/op 1 allocs/op
109+ NewV7() 3859464 308.7 ns/op 16 B/op 1 allocs/op
110+ ```
111+
112+ ## Contributing
113+
114+ Read the [ CONTRIBUTING.md] ( CONTRIBUTING.md ) guide to learn how to contribute to this project.
115+
116+ ## License
117+
118+ This project is licensed under the MIT License - see the [ LICENSE] ( LICENSE ) file for details.
0 commit comments