Skip to content

Instantly share code, notes, and snippets.

@crazed
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crazed/229558cf205f4b69d711 to your computer and use it in GitHub Desktop.
Save crazed/229558cf205f4b69d711 to your computer and use it in GitHub Desktop.
non working go multiwriter example
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