Last active
August 29, 2015 14:10
-
-
Save crazed/229558cf205f4b69d711 to your computer and use it in GitHub Desktop.
non working go multiwriter example
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 ( | |
"net/http" | |
"io" | |
"log" | |
"bufio" | |
) | |
func performRequest(logPrefix string, url string, bodyReader io.Reader) { | |
httpClient := http.Client{} | |
req, err := http.NewRequest("POST", url, bodyReader) | |
if err != nil { | |
panic(err) | |
} | |
log.Println(logPrefix, "Sending off request") | |
resp, err := httpClient.Do(req) | |
if err != nil { | |
panic(err) | |
} | |
defer resp.Body.Close() | |
log.Println(logPrefix, "Finished request!") | |
} | |
func main() { | |
prA, pwA := io.Pipe() | |
//prB, pwB := io.Pipe() | |
debugReader, debugWriter := io.Pipe() | |
writer := io.MultiWriter(pwA, debugWriter) | |
go func(writer io.Writer) { | |
for i := 0; i < 100; i++ { | |
writer.Write([]byte("My cool string\n")) | |
} | |
pwA.Close() | |
}(writer) | |
go func(reader io.Reader) { | |
for { | |
buf := bufio.NewReader(reader) | |
i, _ := buf.ReadBytes('\n') | |
log.Println("DEBUG:", string(i)) | |
} | |
}(debugReader) | |
go performRequest("ONE:", "http://localhost:9999", prA) | |
//go performRequest("TWO:", "http://localhost:8888", ioutil.NopCloser(prB)) | |
// Block it | |
select {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment