https://github.com/neoxelox/enum
Golang Enumerators
https://github.com/neoxelox/enum
enum enumerator enums go go-enum golang neoxelox simple type
Last synced: over 1 year ago
JSON representation
Golang Enumerators
- Host: GitHub
- URL: https://github.com/neoxelox/enum
- Owner: neoxelox
- License: mit
- Created: 2021-03-12T17:28:48.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-05-02T00:30:05.000Z (about 5 years ago)
- Last Synced: 2025-01-09T16:22:30.950Z (over 1 year ago)
- Topics: enum, enumerator, enums, go, go-enum, golang, neoxelox, simple, type
- Language: Go
- Homepage:
- Size: 17.6 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# enum 
**📑 `Golang Enumerators` 📑**
## What
Enum is a package that provides simple enumerators for Go, with IDE autocompletion and any type support. It may not be the prettiest syntax, but it's so useful.
## Install
**`go get github.com/neoxelox/enum`**
## Usage
**`Create Enum`**
```go
package main
import (
"fmt"
"github.com/neoxelox/enum"
)
type State string
type enumStates = struct {
COMMITTED State // You can use any type you want, for example an int.
IN_PROGRESS State
DONE State
enum.Enum
}
var States = enum.New(&enumStates{
COMMITTED: "COMMITTED",
IN_PROGRESS: "BLOCKED",
DONE: "DONE",
}).(*enumStates)
func main() {
fmt.Println(States.DONE)
}
```
Creating a custom type, from a primitive type, for the enum fields (as in the example above), will provide _lite type assertion_, that is, `States.COMMITTED == "COMMITTED"` will evaluate to `true`. If you want full type assertion, you can create `type State struct{ string }`, and use that type as the type for the enum fields `COMMITTED: State{"COMMITTED"}`. Now you can't compare `States.COMMITTED == "COMMITTED"`. However, you will need to create your own `String`, `Marshalling`, `Text`... methods, to deal with serialization correctly.
**`Check if Alias is in Enum`**
```go
States.Is("COMMITTED") // true
States.Is("COMPLETED") // false
States.Is("BLOCKED") // false
```
**`Get Enum Aliases`**
```go
States.Aliases() // [COMMITTED IN_PROGRESS DONE]
```
**`Check if Value is in Enum`**
```go
States.In(State("COMMITTED")) // true
States.In(State("BLOCKED")) // true
States.In(State("IN_PROGRESS")) // false
States.In(State("COMPLETED")) // false
```
**`Get Enum Values`**
```go
States.Values() // [COMMITTED BLOCKED DONE]
```
See [`GoDev`](https://pkg.go.dev/github.com/neoxelox/enum) for further documentation.
## Contribute
Feel free to contribute to this project : ) .
## License
This project is licensed under the [MIT License](https://opensource.org/licenses/MIT) - read the [LICENSE](LICENSE) file for details.