Skip to content

Instantly share code, notes, and snippets.

@picatz
Created August 21, 2018 16:46
Show Gist options
  • Save picatz/a334117a226c79d7545dd9f9c8684fad to your computer and use it in GitHub Desktop.
Save picatz/a334117a226c79d7545dd9f9c8684fad to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"net/http"
grmon "github.com/bcicen/grmon/agent"
)
func newProducer(products ...string) <-chan string {
results := make(chan string)
go func() {
defer close(results)
for _, product := range products {
results <- product
}
}()
return results
}
func exampleEndpoint(w http.ResponseWriter, req *http.Request) {
var counter = 0
for result := range newProducer("A", "B", "C", "E", "F", "G") {
counter++
fmt.Fprintln(w, result)
if counter == 3 {
return
}
}
}
func main() {
http.HandleFunc("/example", exampleEndpoint)
grmon.Start()
log.Fatal(http.ListenAndServe(":8080", nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment