duanlu0386
2017-10-13 18:41
浏览 36
已采纳

为什么我从io.PipeReader中获得EOF?

I’m using something similar in a project and I'm a bit perplexed: why isn't anything being printed?

package main
import (
    "fmt"
    "encoding/json"
    "io"
)

func main() {
    m := make(map[string]string)
    m["foo"] = "bar"

    pr, pw := io.Pipe()
    go func() { pw.CloseWithError(json.NewEncoder(pw).Encode(&m)) }()

    fmt.Fscan(pr)
}

https://play.golang.org/p/OJT1ZRAnut

Is this a race condition of some sort? I tried removing pw.CloseWithError but it changes nothing.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanrong5927 2017-10-13 19:32
    已采纳

    fmt.Fscan takes two arguments. A reader to read from, and one or more pointers to objects to populate. Its result is (n int, err error), where n is the number of items read, and err is the reason why n is less than the (variadic...) slice of data objects you fed into its second argument.

    In this case, the slice of data objects is length zero, so Fscan fills zero objects and reads no data. It dutifully reports that it scanned 0 objects, and since that number is not less than the number of objects you passed into it, it reports nil error.

    Try the following:

    func main() {
        m := make(map[string]string)
        m["foo"] = "bar"
    
        pr, pw := io.Pipe()
        go func() { pw.CloseWithError(json.NewEncoder(pw).Encode(&m)) }()
    
        var s string
        n, err := fmt.Fscan(pr, &s)
        fmt.Println(n, err)  // should be 1 <nil>
        fmt.Println(s)       // should be {"foo":"bar"}
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题