Created
June 5, 2022 11:51
-
-
Save narenaryan/9fac30449a3721989ffd64e8a3148e2f to your computer and use it in GitHub Desktop.
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 ( | |
"fmt" | |
"io" | |
) | |
func main() { | |
pr, pw := io.Pipe() | |
// Writing data to writer should be in a go-routine | |
// because pipe is synchronous. | |
go func() { | |
defer pw.Close() // Important! To notify writing is done | |
fmt.Fprintln(pw, "Hello Medium") | |
}() | |
// Code is blocked until someone writes to writer and closes it | |
b, err := io.ReadAll(pr) | |
if err != nil { | |
panic(err) | |
} | |
// Optional: verify data | |
fmt.Println(string(b)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment