0
我是golang新手,我有問題理解go io.Pipe與node.js Pipe類似嗎?我該如何使用它?是否有可能將它與1個讀取文件和一個寫入文件一起使用?Golang io.Pipe vs node.js管道
在此先感謝傢伙。
我是golang新手,我有問題理解go io.Pipe與node.js Pipe類似嗎?我該如何使用它?是否有可能將它與1個讀取文件和一個寫入文件一起使用?Golang io.Pipe vs node.js管道
在此先感謝傢伙。
不,他們不完全相似。 io.Copy(dat io.Writer, src io.Reader)
相當足夠的讀取和寫入文件,這樣
input := bufio.NewReader(os.Stdin)
output := bufio.NewWriter(os.Stdout) // buffer output like C stdlib
io.Copy(output, input) // copy entire file
output.Flush()
io.Pipe() (*PipeReader, *PipeWriter)
會產生管道Reader和Writer你,當你還沒有他們,但代碼指望他們,這樣
type id struct{
name string
age int
}
payload:=id{"John", 25}
requestBody, jsonPayload := io.Pipe()
request:=http.NewRequest("POST". "http://www.example.com", requestBody) //NewRequest expect io.Reader
encoder:=json.NewEncoder(jsonPayload) //NewEncoder expect io.Writer
err:=encoder.Encode(payload)
response, err:=client.Do(request)
我不能回答這個問題因爲我不知道節點管道是什麼。我可以告訴你,io.Pipe在Go程序中很少使用。我建議你現在跳過io.Pipe,當你有更多的經驗時再回來。 Go程序通常使用[io.Copy](http://godoc.org/io#Copy)將一個文件的內容複製到另一個文件中。 –