Created
December 29, 2016 00:02
-
-
Save miku/b7d75ce03f43ccf42882d3a5683142b6 to your computer and use it in GitHub Desktop.
[Golang] io.Pipe
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
// 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