类型化常量声明列表

发布于 2024-10-17 17:43:42 字数 670 浏览 2 评论 0原文

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

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

枚举基于 枚举fuse_opcode来自FUSE 。这是我想要完成的一些代码(也是非常错误的):

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

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
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抱猫软卧 2024-10-24 17:43:42

你想要这样的东西。您仍然可以将这些常量与文字整数进行比较(无法阻止这种情况),但与其他整数值的任何比较或赋值都会出现编译器错误。

type opCode int

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

如果您确实想阻止外部包看到这些是整数常量的事实,但您仍然希望它们具有可比性,您可以考虑执行类似的操作,

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.

表情可笑 2024-10-24 17:43:42
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
)
赤濁 2024-10-24 17:43:42

这是 FUSE 操作码的 Go 代码。它是从 枚举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
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文