goenum

package module
v0.0.0-...-4c1a12f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2014 License: MIT Imports: 2 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Build Status Coverage License

goenum

Enum for Go

Example

package main

import (
	"fmt"
	"os"
	"strconv"

	"github.com/oinume/goenum"
)

type LangsEnum struct {
	Go     int
	Python int
	Ruby   int
	Haskel int
}

var Langs LangsEnum = LangsEnum{1, 2, 3, 4}

func (e LangsEnum) Enum() goenum.Enum {
	// Make Enum from Langs
	return goenum.EnumerateStruct(&Langs)
}

func main() {
	var langs goenum.Enum = Langs.Enum()

	// Enumerate names
	fmt.Println(langs.Names())
	// --> [Go Python Ruby Haskel]

	// Enumerate values
	fmt.Println(langs.Values())
	// --> [1 2 3 4]

	// Obtain name from value
	fmt.Println(langs.MustName(1))
	// --> Go

	// Obtain value from name
	fmt.Println(langs.MustValue("Python"))
	// --> 2

	if len(os.Args) > 1 {
		// $ go run sample/langs.go 2
		value, _ := strconv.Atoi(os.Args[1])
		switch value {
		case Langs.Go:
			fmt.Println("Gopher")
		case Langs.Python:
			fmt.Println("Pythonista")
		case Langs.Ruby:
			fmt.Println("Rubyist")
		case Langs.Haskel:
			fmt.Println("Haskeler")
		}
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Enum

type Enum struct {
	// contains filtered or unexported fields
}

struct value of Enum

func EnumerateStruct

func EnumerateStruct(target interface{}) Enum

Convert an argument 'target' to goenum.Enum

func (Enum) Alias

func (e Enum) Alias(value int) (string, bool)

Return the alias for given value. panic() if not found.

func (Enum) Aliases

func (e Enum) Aliases() []string

Return enum aliases

func (Enum) MustAlias

func (e Enum) MustAlias(value int) string

func (Enum) MustName

func (e Enum) MustName(value int) string

Return the name for given value. panic() if not found.

func (Enum) MustValue

func (e Enum) MustValue(name string) int

Return the value for given name. panic() if not found.

func (Enum) MustValueForAlias

func (e Enum) MustValueForAlias(alias string) int

Return the value for given alias. panic() if not found.

func (Enum) Name

func (e Enum) Name(value int) (string, bool)

Return the name for given value. Returns empty string and false if not found.

func (Enum) NameValues

func (e Enum) NameValues() map[int]string

Return enum values/names as map

func (Enum) Names

func (e Enum) Names() []string

Return enum names

func (Enum) Value

func (e Enum) Value(name string) (int, bool)

Return the value for given name. Returns -1 and false if not found.

func (Enum) ValueForAlias

func (e Enum) ValueForAlias(alias string) (int, bool)

Return the value for given alias. Returns -1 and false if not found.

func (Enum) Values

func (e Enum) Values() []int

Return enum values

type Enumerator

type Enumerator interface {
	Enum() Enum
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL