This is a creation in Article, where the information may have evolved or changed.
Today we continue to talk about the standard library of Golang IO
[1]type Pipereader
[PHP]
Type Pipereader struct {
Contains filtered or unexported fields
}
[/php]
(1) func Pipe() (*PipeReader, *PipeWriter)
Create a pipeline and return its reader and writer, this will be in-memory pipeline synchronization, its opening will be IO. Reader then waits for the input of the io.writer, without an internal buffer, it is safe to call read and write each other and parallel call writes
[PHP]
Import (
"FMT"
"IO"
"Reflect"
)
Func Main () {
R, W: = Io. Pipe ()
Fmt. Println (reflect. TypeOf (R))//*io. Pipereader
Fmt. Println (reflect. TypeOf (W))//*io. Pipewriter
}
[/php]
(2) func (r *PipeReader) Close() error
After the pipeline is closed, a write operation that is in progress or subsequent writes returns ERRCLOSEDPIPE
[PHP]
Import (
"FMT"
"IO"
)
Func Main () {
R, W: = Io. Pipe ()
R.close ()
_, Err: = W.write ([]byte ("Hello Widuu"))//Can see the func (w *pipewriter) Write below (data []byte) (n int, err error), of course, it is used, it Is behind the blocking concrete introduction
If err = = Io. Errclosedpipe {
Fmt. Println ("Pipeline closed cannot write")//pipeline closed cannot write
}
}
[/php]
(3) func (r *PipeReader) CloseWithError(err error) error
This is the Top R. When close closes, the writer returns the wrong information
[PHP]
Import (
"Errors"
"FMT"
"IO"
)
Func Main () {
R, W: = Io. Pipe ()
R.close ()
ERR: = errors. New ("Pipe break Off")//errors this bag we've already said before, just a way new won't be able to look at the front
R.closewitherror (ERR)
_, Err = W.write ([]byte ("Test"))
If err! = Nil {
Fmt. PRINTLN (ERR)//pipe break off
}
}
[/php]
(4) func (r *PipeReader) Read(data []byte) (n int, err error)
Standard reading interface, which reads data from the pipeline, blocks until a write interface shuts down, and if an error occurs on the write side, it returns an error, otherwise the EOF returned
[PHP]
Import (
"FMT"
"IO"
)
Func Main () {
R, W: = Io. Pipe ()
Go W.write ([]byte ("Hello Widuu"))
D: = make ([]byte, 11)
N, _: = R.read (d)//read data from pipe
Fmt. Println (String (d))
Fmt. PRINTLN (N)
}
[/php]
[2]type Pipewriter
[PHP]
Type Pipewriter struct {
Contains filtered or unexported fields
}
[/php]
(1) func (w *PipeWriter) Close() error
Close the pipe, the read operation that is in progress when it is closed will return EOF, and if there are still unread data in the pipeline, the subsequent reads are still normal
[PHP]
Import (
"FMT"
"IO"
)
Func Main () {
R, W: = Io. Pipe ()
Go W.write ([]byte ("Hello word"))
Data: = Make ([]byte, 10)
N, Err: = R.read (data)
W.close ()
If err = = Io. EOF {
Fmt. PRINTLN ("Executing read return EOF")
Fmt. PRINTLN ("Executing read reads number", N)
}
N, _ = r.read (data)
Fmt. PRINTLN (string data)//hello Word
Fmt. Println ("Next read number", N)//next read number 0
}
[/php]
(2) func (w *PipeWriter) CloseWithError(err error) error
This function and read inside of the closewitherror is similar, close the pipeline, close when the read operation in progress will return the exception of the parameter, if there is still no read data in the pipeline, the subsequent can still read normally
[PHP]
Import (
"Errors"
"FMT"
"IO"
)
Func Main () {
R, W: = Io. Pipe ()
Go W.write ([]byte ("Hello Widuu"))
Newerr: = errors. New ("Your Daye suddenly closed")
W.closewitherror (Newerr)
Data: = Make ([]byte, 10)
_, Err: = R.read (data)
If err! = Nil {
Fmt. PRINTLN (Err)//your Daye suddenly shut down.
}
}
[/php]
(3) func (w *PipeWriter) Write(data []byte) (n int, err error)
Finally hit write, this is to write byte slices to the pipeline, the return is the number of bytes written and error, the front used too much, whichever
[PHP]
Import (
"FMT"
"IO"
)
Func Main () {
R, W: = Io. Pipe ()
Go W.write ([]byte ("" Hello Widuu "))//write is []byte, note that the official document writes that the write pipeline is blocked until the end of all data read
Data: = Make ([]byte, 11)
N, _: = R.read (data)
Fmt. Println (String (data))//hello Widuu
Fmt. PRINTLN ("read number", N)//read Number 10
}
[/php]
Every day only a little Golang standard library, convenient for everyone to learn and use, more time to understand the standard library, we do more hands, if you like please continue to follow us!
Code and files hosted on GitHub, GitHub address https://github.com/widuu/gopkg
Golang Standard Library
Without permission, may not reprint this station any article: the Micro Degree network»golang (go Language) standard library analysis Io (2)