Created
August 21, 2018 16:05
-
-
Save picatz/700ed4fa3c9e8b0e74d5188d6c6be953 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
) | |
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) | |
log.Fatal(http.ListenAndServe(":8080", nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment