Skip to content

Instantly share code, notes, and snippets.

@tkawachi
Created May 29, 2011 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkawachi/997627 to your computer and use it in GitHub Desktop.
Save tkawachi/997627 to your computer and use it in GitHub Desktop.
goroutine test on app engine
package hello
import (
"fmt"
"http"
"time"
)
func init() {
http.HandleFunc("/", handler)
}
func goroutine(n int, w http.ResponseWriter, ch chan int) {
for i := 0; i < 5; i++ {
fmt.Fprintf(w, "Routine %d: %d\n", n, i)
time.Sleep(1000) // sleep 1ms
}
ch <- 0
}
func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
ch := make(chan int)
for i := 0; i < 3; i++ {
go goroutine(i, w, ch)
}
for i := 0; i < 3; i++ {
<-ch
}
fmt.Fprintf(w, "Done.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment