Skip to content

Instantly share code, notes, and snippets.

@ciniglio
Created November 29, 2012 22:30
Show Gist options
  • Save ciniglio/4172343 to your computer and use it in GitHub Desktop.
Save ciniglio/4172343 to your computer and use it in GitHub Desktop.
func main() {
t := NewThing(0)
for i := 0; i < 10; i++ {
inc(t)
}
}
func addOne(i int) {
return i + 1
}
func inc(t Thing) int{
c := make(chan Message)
t.external <- Message{addOne, c}
return <- c
}
type Thing struct {
state int
external chan message
}
type Message struct {
f func(int) int
ret chan int
}
func NewThing(i int) Thing{
t := new(Thing)
t.state = i
t.external = make(chan Message)
go t.run()
return t
}
func (t Thing) run() {
for m := range t.external {
t.i = m.f(t.i)
m.ret <- t.i
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment