GOLANG中是否有“命名空间”枚举?

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

GOLANG "Namespaced" enums?

问题

我理解在Go语言中创建枚举的惯用方式如下:

type topicStatus int

const (
  registered topicStatus = iota
  active
  inactive
  pendingRemoval
  removed
)

但是,如果我有另一个想要“重用”名称的“枚举”,我会遇到错误:

type hotelVisit int

const (
   registered hotelVisit = iota
   checkedIn
   checkedOut
)

在这里,如果我尝试这样做,我无法区分topicStatus.registeredhotelVisit.registered,因为“registered”之前已经被使用过了。是否有一种方法可以为“枚举”名称添加“命名空间”?

英文:

I understand that the idiomatic way to create an enum in GO is as follows:

type topicStatus int

const (
  registered topicStatus = iota
  active
  inactive
  pending-removal
  removed
 )

but if I have another "enum" that wants to "reuse" a name, I get an error:

type hotelVisit int

const (
   registered hotelVisit = iota
   checked-in
   checked-out
)

Here, if I try this, I cannot differentiate between topicStatus.registered and hotelVisit.registered as "registered" was previously used - is there a way to "namespace" the "enum" names?

答案1

得分: 16

污染命名空间并引起命名冲突的做法,不是我认为符合 Go 语言惯用方式的。同样,创建仅用于保存少量常量声明的包也是如此。

我可能会这样做:

type topicStatus int

const (
    tsRegistered topicStatus = iota
    tsActive
    tsInactive
    tsPendingRemoval
    tsRemoved
)

type hotelVisit int

const (
    hvRegistered hotelVisit = iota
    hvCheckedIn
    hvCheckedOut
)

现在你可以使用 ts := tsPendingRemoval 来声明和初始化。清晰简洁,几乎没有命名冲突的风险。

英文:

Polluting the namespace with numerous common word lower case identifiers that are likely to cause naming conflicts isn't something I'd consider idiomatic Go. Same goes for creating packages just to hold a handful of constant declarations.

I'd probably do something like this:

type topicStatus int

const (
    tsRegistered topicStatus = iota
    tsActive
    tsInactive
    tsPendingRemoval
    tsRemoved
)

type hotelVisit int

const (
    hvRegistered hotelVisit = iota
    hvCheckedIn
    hvCheckedOut
)

Now you can declare and initialize with ts := tsPendingRemoval. Clear and concise with little risk of naming conflicts.

答案2

得分: 12

一个解决方法是使用匿名结构体来定义命名空间。

    type TopicStatusType int
    const (
       registered topicStatus = iota
       active
       ...
    )
    var TopicStatus = struct{
        Registered TopicStatusType
        Active TopicStatusType
        ...
    }{
        Registered: registered,
        Active: active,
        ...
    }
英文:

One workaround is to use an anonymous struct to define a namespace.

    type TopicStatusType int
    const (
       registered topicStatus = iota
       active
       ...
    )
    var TopicStatus = struct{
        Registered TopicStatusType
        Active TopicStatusType
        ...
    }{
        Registered: registered,
        Active: active,
        ...
    }

答案3

得分: 11

为了定义枚举,你需要为每个枚举创建一个新的包。这意味着你需要创建一个子目录,并在其中创建一个带有"package topicStatus"的go文件,其中包含常量定义(子目录名称与包名称相同)。
请记住,所有定义的常量都必须大写,以便可以导出。
对于"hotelVisit"和其他你需要的枚举也是同样的操作。
你的程序将导入这些包,并根据需要使用它们:hotelVisit.Registered,topicStatus.Registered。

英文:

Create a new package for each of the enums you want to define. This means creating a sub-directory with a go file the has "package topicStatus" with the const definition inside (sub-directory name is the same as the package name).
Remember all the constants defined must be upper case so they are exportable.
Do the same for "hotelVisit" and whatever you need.
Your program will import these packages and then use them as needed: hotelVisit.Registered, topicStatus.Registered.

答案4

得分: 4

你可以这样做:

var JobRunPhaseConstants = struct {
	QUEUE_PRE_EXEC  int
	JOB_PRE_EXEC    int
	JOB_RUN         int
	JOB_POST_EXEC   int
	QUEUE_POST_EXEC int
}{
	QUEUE_PRE_EXEC:  1,
	JOB_PRE_EXEC:    2,
	JOB_RUN:         3,
	JOB_POST_EXEC:   4,
	QUEUE_POST_EXEC: 5,
}
// 使用示例
var a = JobRunPhaseConstants.JOB_RUN
英文:

You can do like this:

var JobRunPhaseConstants = struct {
	QUEUE_PRE_EXEC  int
	JOB_PRE_EXEC    int
	JOB_RUN         int
	JOB_POST_EXEC   int
	QUEUE_POST_EXEC int
}{
	QUEUE_PRE_EXEC:  1,
	JOB_PRE_EXEC:    2,
	JOB_RUN:         3,
	JOB_POST_EXEC:   4,
	QUEUE_POST_EXEC: 5,
}
// usage example
var a = JobRunPhaseConstants.JOB_RUN

huangapple
  • 本文由 发表于 2014年5月5日 01:24:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/23459224.html
匿名

发表评论

匿名网友

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

确定