Skip to content

Instantly share code, notes, and snippets.

@caelifer
Created December 15, 2016 19:05
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 caelifer/0e348225dcbc85772265d371bcd444d4 to your computer and use it in GitHub Desktop.
Save caelifer/0e348225dcbc85772265d371bcd444d4 to your computer and use it in GitHub Desktop.
// Stolen from https://zupzup.org/io-pipe-go/
pr, pw := io.Pipe()
go func() {
// close the writer, so the reader knows there's no more data
defer pw.Close()
// write json data to the PipeReader through the PipeWriter
if err := json.NewEncoder(pw).Encode(&PayLoad{Content: "Hello Pipe!"}); err != nil {
log.Fatal(err)
}
}()
// JSON from the PipeWriter lands in the PipeReader
// ...and we send it off...
if _, err := http.Post("http://example.com", "application/json", pr); err != nil {
log.Fatal(err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment