2015-01-16 4 views
0

저는 golang을 처음 사용하고 있습니다. io를 이해하는 데 문제가 있습니다 .Pipe는 node.js와 비슷한이 파이프입니까? 어떻게 사용해야합니까? 1 개의 읽기 파일과 1 개의 쓰기 파일과 함께 사용할 수 있습니까?Golang io.Pipe 대 node.js 파이프

미리 감사드립니다.

+1

처럼, 그들에게 기대하는 때 당신을 위해 파이프 리더 및 라이터를 생성합니다 읽고이

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) 같은 파일을 쓸 io.Copy(dat io.Writer, src io.Reader) 매우 충분 노드 파이프가 무엇인지 모르기 때문입니다. 나는 io.Pipe가 Go 프로그램에서 거의 사용되지 않는다고 말할 수 있습니다. 나는 당신이 io.를 건너 뛰고 더 많은 경험이있을 때 다시 돌아올 것을 권한다. 이동 프로그램은 일반적으로 [io.Copy] (http://godoc.org/io#Copy)를 사용하여 한 파일의 내용을 다른 파일로 복사합니다. –

답변

3

아니요 정확하게 유사하지 않습니다. 당신이 그들을하지 않은 있지만, 코드가 난이 질문에 대답 할 수없는이

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)