Search code examples
enumsconstantsgoiota

Typed constant declaration list


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
)

Solution

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