FAQ
io.Pipe doesn't buffer data written to it. In order for pw.Write(...) to
complete there needs to be another goroutine that is calling pr.Read(...).

I modified the provided snippet so that another goroutine reads from the
read-end of the pipe, pr, and writes that data though to os.Stdout. The
program no longer deadlocks.

http://play.golang.org/p/j6X_7aNiGn

I hope that helps. See the io.Pipe docs for more information about its
behavior http://godoc.org/io#Pipe
On Saturday, November 15, 2014 9:49:32 AM UTC-8, Fangdun Cai wrote:


In this case http://play.golang.org/p/i6th-oonic, I use a chan and create
a buffer , everything is ok.

package main

import (
"bytes"
"log"
"mime/multipart"
)

func main() {
files := []string{"readbytes_progress.go", "read_files_chan.go",
"readwriter2.go"}

c := make(chan int64)
b := new(bytes.Buffer)
w := multipart.NewWriter(b)

go func() {
for _, name := range files {
fp, err := w.CreateFormFile("file", name)
log.Println(fp, err)
}
c <- 377
w.Close()
}()
log.Println(<-c)
}

But when i using io.Pipe, *CreatePart *will faild.
http://play.golang.org/p/WKUzEKArAY

package main

import (
"io"
"log"
"mime/multipart"
)

func main() {
files := []string{"readbytes_progress.go", "read_files_chan.go",
"readwriter2.go"}

c := make(chan int64)
pr, pw := io.Pipe()
w := multipart.NewWriter(pw)

go func() {
for _, name := range files {
fp, err := w.CreateFormFile("file", name)
log.Println(fp, err)
}
c <- 377
w.Close()
pw.Close()
}()
log.Println(<-c)
log.Println(pr)
}


I want to use `io.Pipe`, How to fix this? Help me.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 2 of 3 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 16, '14 at 7:14p
activeNov 17, '14 at 2:37a
posts3
users2
websitegolang.org

2 users in discussion

Fangdun Cai: 2 posts Bryan Matsuo: 1 post

People

Translate

site design / logo © 2021 Grokbase