dstjh46606 2019-08-02 13:45
浏览 107
已采纳

代表Go中的状态

We generally use Enums to represent states.

Eg, in python: we do

class QueueState(Enum):
    Enqueued = 1
    Processing = 2
    Processed = 3
    Dequeued = 4

And we can access them using QueueState.Enqueued, etc. The same kind of behavior is present in other languages also, like Java, C#, etc. I mean these states are kinda bound within QueueState.

But when it comes to declaring states in go, we use const and iota, eg:

type QueueState int

const (
    Enqueued QueueState = iota
    Processing
    Processed
    Dequeued
)

I see there is no binding of these states (Enqueued, Processing, etc) with the type QueueState.

To access them, I just need to use them as a constant variable.

Eg:

fmt.Println(Enqueued) // prints 0

Is there a way I can bind these states into a type and treat them as an enum as we do in other programming languages? Eg: I wanted to use them something like this QueueState.Enqueued

  • 写回答

1条回答 默认 最新

  • doze79040 2019-08-02 13:54
    关注

    I see there is no binding of these states (Enqueued, Processing, etc) with the type QueueState.

    This is not completely true. When you print its value, you see 0 printed because that's its numerical value. The type QueueState has int as its underlying type. But Enqueued is of type QueueState (try it on the Go Playground):

    fmt.Printf("%T", Enqueued) // main.QueueState
    

    If you want to "visually" bound it to the QueueState type, include it in its name:

    type QueueState int
    
    const (
        QueueStateEnqueued QueueState = iota
        QueueStateProcessing
        QueueStateProcessed
        QueueStateDequeued
    )
    

    Then when it is referred: QueueStateEnqueued it becomes obvious. This naming "technique" is widely used in the standard library, some examples from 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
            ...
    )
    

    If you want human-readable printed value, define a String() string method for it:

    type QueueState int
    
    func (s QueueState) String() string {
        switch s {
        case QueueStateEnqueued:
            return "Enqueued"
        case QueueStateProcessing:
            return "Processing"
        case QueueStateProcessed:
            return "Processed"
        case QueueStateDequeued:
            return "Dequeued"
        }
        return ""
    }
    

    Then when printed (try it on the Go Playground):

    fmt.Println(QueueStateEnqueued) // prints Enqueued
    

    Yes, it's not very convenient to provide this String() method and keep it updated, hence why tools like stringer exist. They generate this String() method in a more compact and efficient way than the above example implementation.

    There's also the option to use string as the enum's underlying type, and the enum values will serve as the string representation without the String() method (try it on the Go Playground):

    type QueueState string
    
    const (
        QueueStateEnqueued   QueueState = "Enqueued"
        QueueStateProcessing QueueState = "Processing"
        QueueStateProcessed  QueueState = "Processed"
        QueueStateDequeued   QueueState = "Dequeued"
    )
    
    func main() {
        fmt.Println(QueueStateEnqueued) // prints Enqueued
    }
    

    Also note that when others refer to your enum values, they do so using the package name. So you may place the enum constants in their designated package, e.g. called queuestate, and then you may name your constants just Enqueued, Processing etc, but when they are referred to, it will be in the form of queuestate.Enqueued, queuestate.Processing etc.

    Also note that using constants only you can't restrict the values of your type. For details, see Creating a Constant Type and Restricting the Type's Values.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?