Skip to content

Instantly share code, notes, and snippets.

@agupta666
Last active April 30, 2018 10:04
Show Gist options
  • Save agupta666/1b76a75053c0f7c6db58 to your computer and use it in GitHub Desktop.
Save agupta666/1b76a75053c0f7c6db58 to your computer and use it in GitHub Desktop.
io.TeeReader usage
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