英文:
How can I write an enum with "intellisence support"?
问题
在Go语言中,你可以像这样编写一个枚举:
type Direction int
const (
North Direction = iota
South
East
West
)
func main() {
// 声明一个类型为Direction的变量myDirection
var myDirection Direction
myDirection = West
if myDirection == West {
fmt.Println("myDirection is West:", myDirection)
}
}
现在假设你要编写一个不仅有4个选项,而是有100个选项的枚举。我想要的是一个能够提供"智能提示"的枚举:当我输入枚举类型,然后输入一个点号".",我想知道该枚举有哪些选项。
下面是一个示例,展示了如何实现这个功能。是否有更好的方法呢?
type direction struct{}
func (d *direction) north() string {
return "north"
}
func (d *direction) east() string {
return "east"
}
func (d *direction) south() string {
return "south"
}
func (d *direction) west() string {
return "west"
}
func main() {
var d direction
d.east()
...
}
英文:
In go you can write an enum like this
type Direction int
const (
North Direction = iota
South
East
West
)
func main() {
// Declaring a variable myDirection with type Direction
var myDirection Direction
myDirection = West
if (myDirection == West) {
fmt.Println("myDirection is West:", myDirection)
}
}
Now image you write an enum which not only has 4 option, but instead 100. What I want is an enum that gives me "Inellisence support": If I type the enum, type a ., I want to know what options are there for the enum.
An example how this could look like is this. Is there a better way?
type direction struct{}
func (d *direction) north() string {
return "north"
}
func (d *direction) east() string {
return "east"
}
func (d *direction) south() string {
return "south"
}
func (d *direction) west() string {
return "west"
}
func main() {
var d direction
d.east()
...
}
答案1
得分: 1
我建议使用一个共同的前缀来命名枚举值,例如Dir
,像这样:
const (
DirNorth Direction = iota
DirSouth
DirEast
DirWest
)
这样做的好处是,当你输入packagename.Dir
时,你将得到可能的值列表。
因此,除了应用良好的命名策略外,你还将同时获得改进的自动完成功能,使你的源代码更易读(特别是如果枚举值很多,并且它们之间有更多的共同单词)。
标准库也使用了这种方法,一个很好的例子是net/http
包:
const (
MethodGet = "GET"
MethodHead = "HEAD"
MethodPost = "POST"
// ...
)
const (
StatusContinue = 100 // RFC 7231, 6.2.1
StatusSwitchingProtocols = 101 // RFC 7231, 6.2.2
StatusProcessing = 102 // RFC 2518, 10.1
StatusOK = 200 // RFC 7231, 6.3.1
StatusCreated = 201 // RFC 7231, 6.3.2
// ...
)
相关问题请参考:https://stackoverflow.com/questions/39830166/glued-acronyms-and-golang-naming-convention/39830270#39830270
英文:
I suggest to start the names of the enum values with a common prefix, e.g. Dir
like this:
const (
DirNorth Direction = iota
DirSouth
DirEast
DirWest
)
Doing so, when you type packagename.Dir
, you'll get a list of the possible values.
So beside applying a good naming strategy, you'll get improved auto-completion at the same time, and your source code becomes more readable (especially if there are a lot of enum values and you have more common words among them).
This is also used by the standard library, great examples are in the net/http
package:
const (
MethodGet = "GET"
MethodHead = "HEAD"
MethodPost = "POST"
// ...
)
const (
StatusContinue = 100 // RFC 7231, 6.2.1
StatusSwitchingProtocols = 101 // RFC 7231, 6.2.2
StatusProcessing = 102 // RFC 2518, 10.1
StatusOK = 200 // RFC 7231, 6.3.1
StatusCreated = 201 // RFC 7231, 6.3.2
// ...
)
See related question: https://stackoverflow.com/questions/39830166/glued-acronyms-and-golang-naming-convention/39830270#39830270
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论