Created
August 18, 2015 18:10
-
-
Save sgmac/174ae8df779d73b8a2a4 to your computer and use it in GitHub Desktop.
Example using io.TeeReader and 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
package main | |
import ( | |
"bufio" | |
"fmt" | |
"io" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"sync" | |
) | |
var wg sync.WaitGroup | |
func main() { | |
resp, err := http.Get("http://www.get-ip.me/") | |
if err != nil { | |
log.Fatal(err) | |
} | |
pr, pw := io.Pipe() | |
wg.Add(1) | |
go func() { | |
fmt.Println("Writing in pipe writer in goroutine") | |
ioutil.ReadAll(io.TeeReader(resp.Body, pw)) | |
defer func() { | |
resp.Body.Close() | |
pw.Close() | |
wg.Done() | |
}() | |
}() | |
scanner := bufio.NewScanner(pr) | |
for scanner.Scan() { | |
fmt.Println(scanner.Text()) | |
} | |
defer pr.Close() | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment