1 Star 0 Fork 0

c01dface/s-go-example

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
enums.go 1.43 KB
一键复制 编辑 原始数据 按行查看 历史
c01dface 提交于 2024-11-27 19:23 . add enums.go
package main
// Enumerated types (enums) are a special case of sum types. An
// enum is a type that has a fixed number of possible values, each
// with a distinct name. Go doesn't have an enum type as a distinct
// language feature, but enums are simple to implement using existing
// language idioms.
import "fmt"
// our enum type ServerState has an underlying int type
type ServerState int
// The possible values for ServerState are defined as constants.
// The special keyword iota generates successive constant values
// automatically; in this case 0, 1, 2 and so on
const (
StateIdle ServerState = iota
StateConnected
StateError
StateRetrying
)
// By implementing the fmt.Stringer interface, values of
// ServerState can be printed out or converted to strings.
var stateName = map[ServerState]string{
StateIdle: "idle",
StateConnected: "connected",
StateError: "error",
StateRetrying: "retrying",
}
func (ss ServerState) String() string {
return stateName[ss]
}
func main() {
ns := transition(StateIdle)
fmt.Println(ns)
ns2 := transition(ns)
fmt.Println(ns2)
}
// transition emulates a state transition for a server;
// it takes the existing state and returns a new state
func transition(s ServerState) ServerState {
switch s {
case StateIdle:
return StateConnected
case StateConnected, StateRetrying:
return StateIdle
case StateError:
return StateError
default:
panic(fmt.Errorf("unknown state: %s", s))
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/vmalloc/s-go-example.git
git@gitee.com:vmalloc/s-go-example.git
vmalloc
s-go-example
s-go-example
master

搜索帮助