Skip to content

Instantly share code, notes, and snippets.

@kevinlebrun
Created January 6, 2015 12:32
Show Gist options
  • Save kevinlebrun/d07249f3c87205cbf473 to your computer and use it in GitHub Desktop.
Save kevinlebrun/d07249f3c87205cbf473 to your computer and use it in GitHub Desktop.
A simple state machine using recursive type in Go as shared here: http://talks.golang.org/2014/compiling.slide#6
package main
import "fmt"
type F func(*State) F
type State int
func Begin(s *State) F {
*s = 1
return Middle
}
func Middle(s *State) F {
*s++
if *s >= 10 {
return End
}
return Middle
}
func End(s *State) F {
fmt.Println(*s)
return nil
}
func main() {
var f F = Begin
var s State
for f != nil {
f = f(&s)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment