Skip to content

Instantly share code, notes, and snippets.

@chanwit
Created June 19, 2011 11:06
Show Gist options
  • Select an option

  • Save chanwit/1034077 to your computer and use it in GitHub Desktop.

Select an option

Save chanwit/1034077 to your computer and use it in GitHub Desktop.
#!/usr/bin/env gonrun
package main
import "fmt"
type Cmd struct {
Text string
Key string
Data []byte
Reply chan<- *Result
}
type Result struct {
Success bool
Data []byte
}
func main() {
req := make(chan *Cmd, 1)
go func(ch <-chan *Cmd) {
db := map[string][]byte{}
for {
cmd := <-ch
switch cmd.Text {
case "put":
fmt.Printf("put %v\n", cmd.Data)
db[cmd.Key] = cmd.Data
cmd.Reply <- &Result{true, nil}
case "get":
bytes, ok := db[cmd.Key]
cmd.Reply <- &Result{ok, bytes}
}
}
}(req)
go func(){
i := byte(0)
res := make(chan *Result, 1)
for {
i++
req<- &Cmd{"put", "doc1", []byte{i,2,3,4}, res}
<-res
}
}()
res := make(chan *Result, 1)
for {
req <- &Cmd{"get", "doc1", nil, res}
fmt.Printf("got %v\n", <-res)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment