Last active
April 30, 2018 10:04
-
-
Save agupta666/1b76a75053c0f7c6db58 to your computer and use it in GitHub Desktop.
io.TeeReader usage
This file contains 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 ( | |
"net/http" | |
"log" | |
"io" | |
"bufio" | |
"os" | |
) | |
func SaveAndPrint(r io.Reader, fileName string) { | |
file, err := os.Create(fileName) | |
defer file.Close() | |
if err != nil { | |
log.Fatal("ERROR", err) | |
return | |
} | |
w := bufio.NewWriter(file) | |
io.Copy(os.Stdout, io.TeeReader(r, w)) | |
} | |
func main() { | |
resp, err := http.Get("http://www.golang.org") | |
if err != nil { | |
log.Fatal("ERROR", err) | |
} | |
defer resp.Body.Close() | |
SaveAndPrint(resp.Body, "index.html") | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment