代码拉取完成,页面将自动刷新
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))
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。