Git Product home page Git Product logo

enum's Introduction

enum

Easy to use generic named enum library for go.

Introduction

This library implements support for named enums without requiring code generation this is achieved by the obvious approach of requiring the user to give enum names (intead of values) and values (IDs in the context of this library) are auto-generated per associated type starting from 0 and monotonicaly increasing in declaration order.

Creating Enums

Here is how enums are usually created with Go:

type MyType int

const (
    Unknown MyType = iota  // 0
    One                    // 1
    Two                    // 2
)

In the simple example above, enums have no associated names whatsoever and one would have to manually keep track of names if they needed to use those. Libraries like enumer try to solve that by using code generation to add support for keeping track of enum names (plus other niceties).

And here is how they would be created with this library:

type MyType int

var (
    Unknown = enum.New[MyType]("Unknown")  // 0
    One     = enum.New[MyType]("One")      // 1
    Two     = enum.New[MyType]("Two")      // 2
)

Here each enum is associated with a specific type (MyType), has a specific name passed as a parameter to the New call and has an associated auto generated ID.

Using Enums

Below you can find some possibly interesting usage scenarios in the context of enums.

Getting Enum name:

unknownName := Unknown.Name()  // "Unknown"

Getting Enum ID/value:

unknownID := Unknown.ID()  // 0

TODO(bga): Finish this.

enum's People

Contributors

bruno-ga avatar vonc avatar

Forkers

wajeshubham

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.