Created
July 15, 2018 12:50
-
-
Save NaniteFactory/b7968cb0e30d7bc94ba8b6f59e56c6d0 to your computer and use it in GitHub Desktop.
From io.Reader to io.Writer
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" | |
| "io" | |
| "io/ioutil" | |
| "log" | |
| "net/http" | |
| "os" | |
| ) | |
| func main() { | |
| response, err := http.Get("http://nanite.tistory.com") | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| defer response.Body.Close() | |
| // 1_ io.Copy() | |
| // io.Reader -> io.Writer | |
| n, err := io.Copy(os.Stdout, response.Body) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| log.Println("Number of bytes copied to STDOUT:", n) | |
| // 2_ ioutil.ReadAll() | |
| // io.Reader -> []byte | |
| body, err := ioutil.ReadAll(response.Body) | |
| fmt.Println(string(body)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment