Created
May 10, 2016 08:47
-
-
Save kylefeng/c7b6bd6f4dc009313c88616111a433be to your computer and use it in GitHub Desktop.
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 ( | |
"fmt" | |
"io" | |
"io/ioutil" | |
"log" | |
"sync" | |
) | |
var wg sync.WaitGroup | |
func checkError(err error) { | |
if err != nil { | |
log.Fatal("[ERROR] ", err) | |
} | |
} | |
func main() { | |
wg.Add(2) | |
pipR, pipW := io.Pipe() | |
go func() { | |
defer wg.Done() | |
defer pipR.Close() | |
buf, err := ioutil.ReadAll(pipR) | |
checkError(err) | |
fmt.Println("Read() finished, content =", string(buf)) | |
}() | |
go func() { | |
defer wg.Done() | |
defer pipW.Close() | |
n, err := pipW.Write([]byte("Hello World!")) | |
checkError(err) | |
fmt.Println("Write() finished, wrote", n, "bytes") | |
}() | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment