states := []state.State{ state.State{ Name: "idle", OnEnter: func() error { fmt.Println("Entering idle state") return nil }, OnExit: func() error { fmt.Println("Exiting idle state") return nil }, Transitions: []state.Transition{ { Event: "start", Target: "inprogress", }, }, }, state.State{ Name: "inprogress", OnEnter: func() error { fmt.Println("Entering inprogress state") return nil }, OnExit: func() error { fmt.Println("Exiting inprogress state") return nil }, Transitions: []state.Transition{ { Event: "finish", Target: "done", }, { Event: "abort", Target: "idle", }, }, }, state.State{ Name: "done", OnEnter: func() error { fmt.Println("Entering done state") return nil }, OnExit: func() error { fmt.Println("Exiting done state") return nil }, }, } sm := state.NewMachine(states, "idle") err := sm.Start()
err := sm.Trigger("start") if err != nil { fmt.Println("Failed to trigger transition:", err) return }
fmt.Println("Current state:", sm.CurrentState())The package library for the go github.com.juju.juju.state Machine is "github.com/juju/juju/state".