我爱占星网 我爱占星网
首页
编程
java
php
前端
首页 编程 java php 前端

将整数转换为枚举值

I have the following code:

type ActorState int

const (
    Unverified ActorState = 1 + iota
    Verified
    Banned
)

And I want to do this in my implementation.

i := 1
a : ActorState 
a = ActorState(i)

But I get the error, Cannot convert expression of type int, error to type ActorState.

How do I convert it then?

Your syntax is wrong, you mean

i := 1
a := ActorState(i)

or

i := 1
var a = ActorState(i)

or

i := 1
var a ActorState
a = ActorState(i)

近期文章

  • Go struct方法允许类型混合吗?
  • 返回404的多路复用大猩猩路线
  • 将字符串值转换为函数调用
  • Golang SQL查询变量替换
  • 在Golang中封装`sort`接口
  • 在Go-lang中返回对struct的引用
  • 如何通过Gmail Go SDK发送电子邮件?
  • ZeroMQ中的PUB / SUB模式不起作用
  • 如何在运行时读取文本文件
  • Golang可以安全地切换cmd.Stdout
  • UDP在golang中,监听不是阻止呼叫吗?
  • 解释SetWriteDeadline错误
  • 如何让Heroku检测GO buildpack
  • 使用Golang读取随机内存位置
  • MapReduce中的Golang GlobalSign mgo查询
  • Go-Python的“ pass”等效于什么?
  • 代理https时强制客户端主机名
  • GoLang在字节片的第N行获取字符串
  • 编译器错误将切片附加到切片
  • 区分JSON和其他错误

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

部分图文来自网络,如有侵犯您的版权,请告诉我们删除

友情链接:代码精华