Code Examples
package main
import (
"fmt"
"github.com/orsinium-labs/enum"
)
func main() {
type Color enum.Member[string]
var (
Red = Color{"red"}
Green = Color{"green"}
Blue = Color{"blue"}
Colors = enum.New(Red, Green, Blue)
)
contains := Colors.Contains(Red)
fmt.Println(contains)
}
package main
import (
"fmt"
"github.com/orsinium-labs/enum"
)
func main() {
type Color enum.Member[string]
var (
Red = Color{"red"}
Green = Color{"green"}
Blue = Color{"blue"}
Colors = enum.New(Red, Green, Blue)
)
empty := Colors.Empty()
fmt.Println(empty)
}
package main
import (
"fmt"
"github.com/orsinium-labs/enum"
)
func main() {
type Color enum.Member[string]
var (
Red = Color{"red"}
Green = Color{"green"}
Blue = Color{"blue"}
Colors = enum.New(Red, Green, Blue)
)
fmt.Printf("%#v\n", Colors)
}
package main
import (
"fmt"
"github.com/orsinium-labs/enum"
)
func main() {
type Color enum.Member[string]
var (
Red = Color{"red"}
Green = Color{"green"}
Blue = Color{"blue"}
Colors = enum.New(Red, Green, Blue)
)
index := Colors.Index(Green)
fmt.Println(index)
}
package main
import (
"fmt"
"github.com/orsinium-labs/enum"
)
func main() {
type Color enum.Member[string]
var (
Red = Color{"red"}
Green = Color{"green"}
Blue = Color{"blue"}
Colors = enum.New(Red, Green, Blue)
)
length := Colors.Len()
fmt.Println(length)
}
package main
import (
"fmt"
"github.com/orsinium-labs/enum"
)
func main() {
type Color enum.Member[string]
var (
Red = Color{"red"}
Green = Color{"green"}
Blue = Color{"blue"}
Colors = enum.New(Red, Green, Blue)
)
members := Colors.Members()
fmt.Println(members)
}
package main
import (
"fmt"
"github.com/orsinium-labs/enum"
)
func main() {
type Color enum.Member[string]
var (
Red = Color{"red"}
Green = Color{"green"}
Blue = Color{"blue"}
Colors = enum.New(Red, Green, Blue)
)
parsed := Colors.Parse("red")
fmt.Printf("%#v\n", parsed)
}
package main
import (
"fmt"
"github.com/orsinium-labs/enum"
)
func main() {
type Color enum.Member[string]
var (
Red = Color{"red"}
Green = Color{"green"}
Blue = Color{"blue"}
Colors = enum.New(Red, Green, Blue)
)
fmt.Println(Colors)
}
package main
import (
"fmt"
"github.com/orsinium-labs/enum"
)
func main() {
type Color enum.Member[string]
var (
Red = Color{"red"}
Green = Color{"green"}
Blue = Color{"blue"}
Colors = enum.New(Red, Green, Blue)
)
tname := Colors.TypeName()
fmt.Println(tname)
}
package main
import (
"fmt"
"github.com/orsinium-labs/enum"
)
func main() {
type Color enum.Member[string]
var (
Red = Color{"red"}
Green = Color{"green"}
Blue = Color{"blue"}
Colors = enum.New(Red, Green, Blue)
)
value := Colors.Value(Green)
fmt.Println(value)
}
package main
import (
"fmt"
"github.com/orsinium-labs/enum"
)
func main() {
type Color enum.Member[string]
var (
Red = Color{"red"}
Green = Color{"green"}
Blue = Color{"blue"}
Colors = enum.New(Red, Green, Blue)
)
values := Colors.Values()
fmt.Println(values)
}
package main
import (
"fmt"
"github.com/orsinium-labs/enum"
)
func main() {
type Color enum.Member[string]
var (
Red = Color{"red"}
Green = Color{"green"}
Blue = Color{"blue"}
Colors = enum.New(Red, Green, Blue)
)
fmt.Printf("%#v\n", Colors)
}
package main
import (
"fmt"
"github.com/orsinium-labs/enum"
)
func main() {
type Color enum.Member[string]
var (
b = enum.NewBuilder[string, Color]()
Red = b.Add(Color{"red"})
Green = b.Add(Color{"green"})
Blue = b.Add(Color{"blue"})
Colors = b.Enum()
)
fmt.Println(
Colors.Contains(Red),
Colors.Contains(Green),
Colors.Contains(Blue),
)
}
package main
import (
"fmt"
"strings"
"github.com/orsinium-labs/enum"
)
type FoldedString string
// Equal implements [enum.Equaler].
//
// Compare strings ignoring the case.
func (s FoldedString) Equal(other FoldedString) bool {
return strings.EqualFold(string(s), string(other))
}
func main() {
type Color enum.Member[FoldedString]
var (
Red = Color{"red"}
Green = Color{"green"}
Blue = Color{"blue"}
Colors = enum.New(Red, Green, Blue)
)
parsed := enum.Parse(Colors, "RED")
fmt.Printf("%#v\n", parsed)
}
Package-Level Type Names (total 4)
/* sort by: | */
Type Parameters:
M: iMember[V]
V: comparable Builder is a constructor for an [Enum].
Use [Builder.Add] to add new members to the future enum
and then call [Builder.Enum] to create a new [Enum] with all added members.
Builder is useful for when you have lots of enum members, and new ones
are added over time, as the project grows. In such scenario, it's easy to forget
to add in the [Enum] a newly created [Member].
The builder is designed to prevent that. Add registers a new [Member] in the builder. Enum creates a new [Enum] with all members registered using [Builder.Add].
func NewBuilder[V, M]() Builder[M, V]
Type Parameters:
M: iMember[V]
V: comparable Enum is a collection of enum members.
Use [New] to construct a new Enum from a list of members. Choice returns a randomly selected member of the enum.
A random seed can be given (or be 0 to use time.Now().UnixNano() as the seed).
nil is returned only if the Enum contains no members. Contains returns true if the enum has the given member. Empty returns true if the enum doesn't have any members. GoString implements [fmt.GoStringer] interface.
When you print a member using "%#v" format,
it will show the enum representation as a valid Go syntax. Index returns the index of the given member in the enum.
If the given member is not in the enum, it panics.
Use [Enum.Contains] first if you don't know for sure
if the member belongs to the enum. Len returns how many members the enum has. Members returns a slice of the members in the enum. Parse converts a raw value into a member of the enum.
If none of the enum members has the given value, nil is returned. String implements [fmt.Stringer] interface.
It returns a comma-separated list of values of the enum members. TypeName is a string representation of the wrapped type. Value returns the wrapped value of the given enum member. Values returns a slice of values of all members of the enum.
Enum : github.com/apache/arrow-go/v18/arrow/compute.FunctionOptions
Enum : expvar.Var
Enum : fmt.GoStringer
Enum : fmt.Stringer
func New[V, M](members ...M) Enum[M, V]
func (*Builder)[M, V].Enum() Enum[M, V]
func Parse[M, V](e Enum[M, V], value V) *M
var github.com/pancsta/asyncmachine-go/pkg/integrations.KindEnumenum.Enum[...]
var github.com/pancsta/asyncmachine-go/pkg/rpc.ClientMethodsenum.Enum[...]
var github.com/pancsta/asyncmachine-go/pkg/rpc.ServerMethodsenum.Enum[...]
Type Parameters:
V: comparable Equaler check if the two values of the same type are equal.( Equaler[V]) Equal(other V) bool
Type Parameters:
T: comparable Member is an enum member, a specific value bound to a variable.ValueT
Package-Level Functions (total 3)
Type Parameters:
V: comparable
M: iMember[V]
New constructs a new [Enum] wrapping the given enum members.
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.