feat: struct embedding, enums

This commit is contained in:
2026-03-02 13:50:35 +01:00
parent cf47b70aae
commit 2ce943f94e
2 changed files with 86 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
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",
}
// String Changes the output value
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))
}
}