How can I write an enum with "intellisence support"?

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

How can I write an enum with "intellisence support"?

问题

在Go语言中,你可以像这样编写一个枚举:

  1. type Direction int
  2. const (
  3. North Direction = iota
  4. South
  5. East
  6. West
  7. )
  8. func main() {
  9. // 声明一个类型为Direction的变量myDirection
  10. var myDirection Direction
  11. myDirection = West
  12. if myDirection == West {
  13. fmt.Println("myDirection is West:", myDirection)
  14. }
  15. }

现在假设你要编写一个不仅有4个选项,而是有100个选项的枚举。我想要的是一个能够提供"智能提示"的枚举:当我输入枚举类型,然后输入一个点号".",我想知道该枚举有哪些选项。

下面是一个示例,展示了如何实现这个功能。是否有更好的方法呢?

  1. type direction struct{}
  2. func (d *direction) north() string {
  3. return "north"
  4. }
  5. func (d *direction) east() string {
  6. return "east"
  7. }
  8. func (d *direction) south() string {
  9. return "south"
  10. }
  11. func (d *direction) west() string {
  12. return "west"
  13. }
  14. func main() {
  15. var d direction
  16. d.east()
  17. ...
  18. }
英文:

In go you can write an enum like this

  1. type Direction int
  2. const (
  3. North Direction = iota
  4. South
  5. East
  6. West
  7. )
  8. func main() {
  9. // Declaring a variable myDirection with type Direction
  10. var myDirection Direction
  11. myDirection = West
  12. if (myDirection == West) {
  13. fmt.Println("myDirection is West:", myDirection)
  14. }
  15. }

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?

  1. type direction struct{}
  2. func (d *direction) north() string {
  3. return "north"
  4. }
  5. func (d *direction) east() string {
  6. return "east"
  7. }
  8. func (d *direction) south() string {
  9. return "south"
  10. }
  11. func (d *direction) west() string {
  12. return "west"
  13. }
  14. func main() {
  15. var d direction
  16. d.east()
  17. ...
  18. }

答案1

得分: 1

我建议使用一个共同的前缀来命名枚举值,例如Dir,像这样:

  1. const (
  2. DirNorth Direction = iota
  3. DirSouth
  4. DirEast
  5. DirWest
  6. )

这样做的好处是,当你输入packagename.Dir时,你将得到可能的值列表。

因此,除了应用良好的命名策略外,你还将同时获得改进的自动完成功能,使你的源代码更易读(特别是如果枚举值很多,并且它们之间有更多的共同单词)。

标准库也使用了这种方法,一个很好的例子是net/http包:

  1. const (
  2. MethodGet = "GET"
  3. MethodHead = "HEAD"
  4. MethodPost = "POST"
  5. // ...
  6. )
  7. const (
  8. StatusContinue = 100 // RFC 7231, 6.2.1
  9. StatusSwitchingProtocols = 101 // RFC 7231, 6.2.2
  10. StatusProcessing = 102 // RFC 2518, 10.1
  11. StatusOK = 200 // RFC 7231, 6.3.1
  12. StatusCreated = 201 // RFC 7231, 6.3.2
  13. // ...
  14. )

相关问题请参考: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:

  1. const (
  2. DirNorth Direction = iota
  3. DirSouth
  4. DirEast
  5. DirWest
  6. )

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:

  1. const (
  2. MethodGet = "GET"
  3. MethodHead = "HEAD"
  4. MethodPost = "POST"
  5. // ...
  6. )
  7. const (
  8. StatusContinue = 100 // RFC 7231, 6.2.1
  9. StatusSwitchingProtocols = 101 // RFC 7231, 6.2.2
  10. StatusProcessing = 102 // RFC 2518, 10.1
  11. StatusOK = 200 // RFC 7231, 6.3.1
  12. StatusCreated = 201 // RFC 7231, 6.3.2
  13. // ...
  14. )

See related question: https://stackoverflow.com/questions/39830166/glued-acronyms-and-golang-naming-convention/39830270#39830270

huangapple
  • 本文由 发表于 2021年12月3日 21:19:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/70214974.html
匿名

发表评论

匿名网友

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

确定