认为有一点混乱,但您正在创建的常量的基础类型是字符串,而不是整数。所以使用int(Created)会给你一个cannot convert Created (type Status) to type int错误。


如果您在使用 Enum 并希望将字符串值与之关联 - 您应该按照您的建议使用 Stringer 接口,简单的方法如下:


type Status int


func (s Status) String() string {

    switch s {

    case 0:

        return "Created"

    case 1:

        return "Deleted"

    default:

        return ""

    }

}


const (

    Created Status = iota

    Deleted

)

然后,任何时候您需要 Status 的字符串版本,您都可以调用string(Created)或简单地传递它(即fmt.Println(Created))