4
4
[ ![ GitHub release (release name instead of tag name)] ( https://img.shields.io/github/v/release/xybor-x/enum?include_prereleases )] ( https://github.com/xybor-x/enum/releases/latest )
5
5
[ ![ GitHub Repo stars] ( https://img.shields.io/github/stars/xybor-x/enum?color=blue )] ( https://github.com/xybor-x/enum )
6
6
7
-
8
7
![ Golang] ( ./.github/go-enum.png )
9
8
10
9
# ⚙️ Go Enum
19
18
[ 6 ] : #-serialization-and-deserialization
20
19
[ 7 ] : #-type-safety
21
20
22
- > [ !WARNING]
23
- > Please keep in mind that ` xybor-x/enum ` is still under active development
24
- > and therefore full backward compatibility is not guaranteed before reaching v1.0.0.
25
-
26
21
## 🔧 Installation
27
22
28
23
``` sh
29
24
go get -u github.com/xybor-x/enum
30
25
```
31
26
27
+ ## ⚡ Quick start
28
+
29
+ > [ !TIP]
30
+ > This is just a ⚡ quick definition for general use cases.
31
+ > See more advanced [ Features] ( #-features ) .
32
+
33
+ > [ !CAUTION]
34
+ > Enum definitions are not thread-safe.
35
+ > Therefore, they should be finalized during initialization (at the global scope).
36
+
37
+
38
+ ``` go
39
+ package main
40
+
41
+ type role any
42
+ type Role = enum.SafeEnum [role]
43
+
44
+ var (
45
+ RoleUser = enum.New [Role](" user" )
46
+ RoleAdmin = enum.New [Role](" admin" )
47
+ _ = enum.Finalize [Role]()
48
+ )
49
+
50
+ func main () {
51
+ // Print out the string representation of enum.
52
+ fmt.Println (RoleAdmin) // Output: admin
53
+
54
+ // Serialize a user to json format.
55
+ data , _ := json.Marshal (RoleUser)
56
+ fmt.Println (string (data)) // Output: "user"
57
+ }
58
+ ```
59
+
32
60
## 📋 Features
33
61
34
62
> [ !TIP]
@@ -41,11 +69,6 @@ go get -u github.com/xybor-x/enum
41
69
| ** Serialization and deserialization** ([ #] [ 6 ] ) | No | ** Yes** | ** Yes** |
42
70
| ** Type safety** ([ #] [ 7 ] ) | No | Basic | ** Strong** |
43
71
44
- > [ !CAUTION]
45
- > Enum definitions are not thread-safe.
46
- > Therefore, they should be finalized during initialization (at the global scope).
47
-
48
-
49
72
## ⭐ Basic enum
50
73
51
74
The basic enum (a.k.a ` iota ` enum) is the most commonly used enum implementation in Go.
@@ -106,11 +129,6 @@ func init() {
106
129
enum.Finalize [Role]() // Optional: ensure no new enum values can be added to Role.
107
130
}
108
131
109
- type User struct {
110
- ID int ` json:"id"`
111
- Role Role ` json:"role"`
112
- }
113
-
114
132
func main () {
115
133
// WrapEnum has many built-in methods for handling enum easier.
116
134
data , _ := json.Marshal (RoleUser) // Output: "user"
@@ -140,8 +158,8 @@ type role any
140
158
type Role = enum.SafeEnum [role] // NOTE: It must use type alias instead of type definition.
141
159
142
160
var (
143
- RoleUser = enum.NewSafe [Role](" user" )
144
- RoleAdmin = enum.NewSafe [Role](" admin" )
161
+ RoleUser = enum.New [Role](" user" )
162
+ RoleAdmin = enum.New [Role](" admin" )
145
163
)
146
164
147
165
func main () {
@@ -301,9 +319,9 @@ type role any
301
319
type Role struct { enum.SafeEnum [role] }
302
320
303
321
var (
304
- RoleUser = enum.NewExtendedSafe [Role](" user" )
305
- RoleMod = enum.NewExtendedSafe [Role](" mod" )
306
- RoleAdmin = enum.NewExtendedSafe [Role](" admin" )
322
+ RoleUser = enum.NewExtended [Role](" user" )
323
+ RoleMod = enum.NewExtended [Role](" mod" )
324
+ RoleAdmin = enum.NewExtended [Role](" admin" )
307
325
_ = enum.Finalize [Role]()
308
326
)
309
327
0 commit comments