Typed constant declaration list

Matt Joiner picture Matt Joiner · Feb 18, 2011 · Viewed 8.5k times · Source

I wish to create an "enum-like" list of constants with the following properties:

  1. The values of each identifier are sequential, with a few gaps. (I believe iota and the blank identifier help in this regard).
  2. The identifiers are private to the module.
  3. The constants can only be compared with other constants of the same type.

The enumeration is based on the enum fuse_opcode from FUSE. Here's some code for what I'm trying to accomplish (and also very wrong):

const Opcode (
    _ = iota // skip 0
    lookupOp
    forgetOp
    getattrOp
    setattrOp
    readlinkOp
    symlinkOp // 6
    _ // skip 7
    mknodOp // 8
    // et cetera ad nauseam
)

Answer

rog picture rog · Feb 18, 2011

You want something like this. You can still compare these constants against literal integers (there's no way to prevent that) but any comparison or assignment to other integer values will get a compiler error.

type opCode int

const (
    lookupOp opCode = iota+1
    forgetOp
    getattrOp
    setattrOp
    readlinkOp
    symlinkOp // 6
    _         // skip 7
    mknodOp   // 8
    // et cetera ad nauseam
)

If you really want to prevent external packages from seeing the fact that these are integer constants, but you still want it comparable, you might consider doing something like this,

type OpCode struct {
    code opCode
}

and only exposing OpCode in your API. I'd also suggest explicitly documenting that it's comparable.