Skip to content

Enhancement: enumeration functions #3

@rickb777

Description

@rickb777

Stringer has knowledge of the set of values in a Go enumeration, with which the String() function is generated.

A more general use-case is to see String() as one of several enumeration support functions. This could add really useful enumerations to Go, based on the pattern of declaring a type based on int and a corresponding set of constants of the type.

We would need:

  • func (s Xxx) String() string - as exists
  • func AllXxxs() [Xxx] - returns a slice containing all const values
  • func XxxOf(string) (Xxx, err) - returns the value matched from a string, if possible

The two new functions are natural extensions of stringer because they're based on having parsed the source code. However, they broaden its purpose considerably.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions