Typed constant declaration list

8.7k views Asked by At

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
)
3

There are 3 answers

4
peterSO On BEST ANSWER

Here's the Go code for the FUSE opcodes. It was created from enum fuse_opcode. Typically you would write a script to do that; I used a text editor. Since the constant values match the C enum values, explicit values are used.

package fuse

type opCode int32

const (
    opLookup      = 1
    opForget      = 2
    opGetattr     = 3
    opSetattr     = 4
    opReadlink    = 5
    opSymlink     = 6
    opMknod       = 8
    opMkdir       = 9
    opUnlink      = 10
    opRmdir       = 11
    opRename      = 12
    opLink        = 13
    opOpen        = 14
    opRead        = 15
    opWrite       = 16
    opStatfs      = 17
    opRelease     = 18
    opFsync       = 20
    opSetxattr    = 21
    opGetxattr    = 22
    opListxattr   = 23
    opRemovexattr = 24
    opFlush       = 25
    opInit        = 26
    opOpendir     = 27
    opReaddir     = 28
    opReleasedir  = 29
    opFsyncdir    = 30
    opGetlk       = 31
    opSetlk       = 32
    opSetlkw      = 33
    opAccess      = 34
    opCreate      = 35
    opInterrupt   = 36
    opBmap        = 37
    opDestroy     = 38
    opIoctl       = 39
    opPoll        = 40
    opNotifyReply = 41
)
6
Jesse On
package fuse

type opCode int32

const (
    opLookup  opCode    = 1
    opForget  opCode    = 2
    opGetattr opCode    = 3
    opSetattr  opCode   = 4
    opReadlink opCode   = 5
    opSymlink  opCode   = 6
    opMknod   opCode    = 8
    opMkdir   opCode    = 9
    opUnlink   opCode   = 10
)
4
rog On

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.