I wish to create an "enum-like" list of constants with the following properties:
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
)
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.