2
1
Fork 0
main
Sebastian Steger 2025-08-20 06:36:25 +00:00
parent 51691f2c19
commit 17d979d0ce
1 changed files with 45 additions and 0 deletions

View File

@ -0,0 +1,45 @@
package main
import "fmt"
type ServerState int
const (
StateIdle ServerState = iota
StateConnected
StateError
StateRetrying
)
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)
}
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))
}
}