类型化常量声明列表

huangapple go评论124阅读模式
英文:

Typed constant declaration list

问题

我希望创建一个类似于“枚举”的常量列表,具有以下特性:

  1. 每个标识符的值是连续的,但有一些间隔。(我相信 iota 和空白标识符在这方面有帮助)。
  2. 标识符对模块是私有的。
  3. 常量只能与相同类型的其他常量进行比较。

这个枚举基于FUSE中的enum fuse_opcode。以下是我尝试实现的代码(也是错误的):

const Opcode (
    _ = iota // 跳过 0
    lookupOp
    forgetOp
    getattrOp
    setattrOp
    readlinkOp
    symlinkOp // 6
    _ // 跳过 7
    mknodOp // 8
    // 等等,以此类推
)
英文:

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
)

答案1

得分: 22

你想要这样的东西。你仍然可以将这些常量与字面整数进行比较(无法阻止这种情况),但是对其他整数值的比较或赋值将导致编译错误。

type opCode int

const (
	lookupOp opCode = iota+1
	forgetOp
	getattrOp
	setattrOp
	readlinkOp
	symlinkOp // 6
	_         // 跳过 7
	mknodOp   // 8
	// 等等,以此类推
)

如果你真的想要防止外部包看到这些是整数常量的事实,但仍然希望它是可比较的,你可以考虑这样做,

type OpCode struct {
    code opCode
}

并且只在你的API中公开 OpCode。我还建议明确说明它是可比较的。

英文:

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.

答案2

得分: 5

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
)

英文:
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
)

答案3

得分: 0

这是FUSE操作码的Go代码。它是从enum fuse_opcode创建的。通常你会编写一个脚本来完成这个任务,我使用了一个文本编辑器。由于常量值与C枚举值匹配,所以使用了显式值。

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
)
英文:

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
)

huangapple
  • 本文由 发表于 2011年2月18日 12:27:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/5037610.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定