Skip to content

Instantly share code, notes, and snippets.

@miku
Created December 29, 2016 00:02
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 miku/b7d75ce03f43ccf42882d3a5683142b6 to your computer and use it in GitHub Desktop.
Save miku/b7d75ce03f43ccf42882d3a5683142b6 to your computer and use it in GitHub Desktop.
[Golang] io.Pipe
// Pipe creates a synchronous in-memory pipe.
// It can be used to connect code expecting an io.Reader
// with code expecting an io.Writer.
// Reads on one end are matched with writes on the other,
// copying data directly between the two; there is no internal buffering.
// It is safe to call Read and Write in parallel with each other or with
// Close. Close will complete once pending I/O is done. Parallel calls to
// Read, and parallel calls to Write, are also safe:
// the individual calls will be gated sequentially.
package main
import (
"encoding/json"
"io"
"log"
"net/http"
"os"
)
func main() {
r, w := io.Pipe()
go func() {
defer w.Close()
json.NewEncoder(w).Encode(map[string]string{
"Name": "Mohanson",
})
}()
resp, err := http.Post("https://httpbin.org/post", "application/json", r)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if _, err := io.Copy(os.Stdout, resp.Body); err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment