In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I will talk to you about the IO method of GO language, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Type PipeWriter
The copy code is as follows:
Type PipeWriter struct {
/ / contains filtered or unexported fields
}
(1) func (w * PipeWriter) Close () error closes the pipe, and the ongoing Read operation will return EOF when it is closed. If there is still unread data in the pipeline, it can still be read normally.
The copy code is as follows:
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
}
(2) the function func (w * PipeWriter) CloseWithError (err error) error is more or less the same as the CloseWithError in read. If the pipeline is closed, the exception passed in the parameter will be returned if the Read operation is being performed when the pipeline is closed. If there is still unread data in the pipeline, it can still be read normally later.
The copy code is as follows:
Import (
"errors"
"fmt"
"io"
)
Func main () {
R, w: = io.Pipe ()
Go w.Write ([] byte ("hello widuu"))
Newerr: = errors.New ("your daye suddenly shut down")
W.CloseWithError (newerr)
Data: = make ([] byte, 10)
_, err: = r.Read (data)
If err! = nil {
Fmt.Println (err) / / your daye suddenly shut down
}
}
(3) func (w * PipeWriter) Write (data [] byte) (n int, err error) finally came to type write. This is to write the byte slice into the pipe and return the number of bytes and error. Too many bytes are used before, either one.
The copy code is as follows:
Import (
"fmt"
"io"
)
Func main () {
R, w: = io.Pipe ()
Go w.Write ([] byte ("hello widuu")) / / writes [] byte. Note that the official document says that the write pipeline is blocked until all data is read.
Data: = make ([] byte, 11)
N, _: = r.Read (data)
Fmt.Println (string (data)) / / hello widuu
Fmt.Println ("read number", n) / / read number 10
}
Type Reader
The copy code is as follows:
Type Reader interface {
Read (p [] byte) (n int, err error)
}
(1) func LimitReader (r Reader, n int64) Reader, we talked about the structure of Reader before. In fact, this is an encapsulation of Reader, limiting the number of bytes it reads. In fact, it implements the structure of io.LimitedReader {}.
The copy code is as follows:
Import (
"fmt"
"io"
"os"
"reflect"
)
Func main () {
F, _: = os.Open ("test.txt")
Defer f.Close ()
Reader: = io.LimitReader (f, 5)
P: = make ([] byte, 5)
Fmt.Println (reflect.TypeOf (reader)) / / * io.LimitedReader
Var total int
For {
N, err: = reader.Read (p)
If err = = io.EOF {
Fmt.Println ("read value", string (p [: total]) / / read value hello
Fmt.Println (total) / / 5
Break
}
Total = total + n
}
}
(2) func MultiReader (readers... Reader) Reader is a function that encapsulates multiple readers at a glance, which is similar to the above method, except that it encapsulates multiple readers. Of course, it also removes the restriction of reading. Let's test it for everyone.
The copy code is as follows:
Import (
"fmt"
"io"
"os"
"reflect"
)
Func main () {
F1, _: = os.Open ("test1.txt")
F2, _: = os.Open ("test.txt")
Defer f1.Close ()
Defer f2.Close ()
Reader: = io.MultiReader (F1, f2) / / * io.multiReader
Fmt.Println (reflect.TypeOf (reader))
P: = make ([] byte, 10)
Var total int
Var data string
For {
N, err: = reader.Read (p)
If err = = io.EOF {
Fmt.Println ("read end", total) / / read end 17
Break
}
Total = total + n
Data = data + string (p [: n])
}
Fmt.Println ("read value", data) / / read value widuu2hello widuu
Fmt.Println ("read count", total) / / read count 17
}
(3) since the above introduction has been read, I would like to introduce type Write`func MultiWriter (writers... Writer) Writer, but this time it will be written instead.
The copy code is as follows:
Import (
"fmt"
"io"
"io/ioutil"
"os"
)
Func main () {
F1, _: = os.Create ("1.txt")
F2, _: = os.Create ("2.txt")
Writer: = io.MultiWriter (F1, f2)
Writer.Write ([] byte ("widuu"))
/ / Don't be so logical. It's for testing.
R1, _: = ioutil.ReadFile ("1.txt")
R2, _: = ioutil.ReadFile ("2.txt")
Fmt.Println (string (R1)) / / widuu
Fmt.Println (string (R2)) / / widuu
}
(4) func TeeReader (r Reader, w Writer) Reader this method is interesting to read data from r and write it to w. There is no internal buffer. Take a look at the code.
The copy code is as follows:
Import (
"fmt"
"io"
"os"
"reflect"
)
Func main () {
R, _: = os.Open ("test.txt")
W, _: = os.Create ("test2.txt")
Reader: = io.TeeReader (r, w)
Fmt.Println (reflect.TypeOf (reader)) / / * io.teeReader
P: = make ([] byte, 10)
N, _: = reader.Read (p)
Fmt.Println (string (p [: n])) / / hello widu
}
Type SectionReader {}
The copy code is as follows:
Type SectionReader struct {
/ / contains filtered or unexported fields
}
(1) func NewSectionReader (r ReaderAt, off int64, n int64) * SectionReader, you can see at a glance that this is the way to get io.SectionReader, the first parameter reader, the second parameter offset, and the third parameter is how much to read.
The copy code is as follows:
Import (
"fmt"
"io"
"os"
"reflect"
)
Func main () {
F, _: = os.Open ("test.txt")
Sr: = io.NewSectionReader (f, 2, 5)
Fmt.Println (reflect.TypeOf (sr)) / / * io.SectionReader
}
(2) the familiar read () of func (s * SectionReader) Read (p [] byte) (n int, err error) is actually used to read data. You can understand it by looking at the function, because we often encounter that both of the last two write this.
The copy code is as follows:
Import (
"fmt"
"io"
"os"
)
Func main () {
F, _: = os.Open ("test.txt")
Defer f.Close ()
Sr: = io.NewSectionReader (f, 2, 5)
P: = make ([] byte, 10)
N, err: = sr.Read (p)
If err! = nil {
Fmt.Println (err)
}
Fmt.Println (string (p [: n])) / / llo w
}
(3) func (s * SectionReader) ReadAt (p [] byte, off int64) (n int, err error) this is the same as the previous ReadAt, except that there is only one offset and less intercept, but you need to know what SectionReader does to intercept the data, so there is no need to intercept
The copy code is as follows:
Import (
"fmt"
"io"
"os"
)
Func main () {
F, _: = os.Open ("test.txt")
Defer f.Close ()
Sr: = io.NewSectionReader (f, 2, 5)
P: = make ([] byte, 10)
N, err: = sr.ReadAt (p, 1)
If err = = io.EOF {
Fmt.Println (string (p [: n])) / / lo w
}
}
(4) func (s * SectionReader) Seek (offset int64, whence int) (int64, error) this is used to set the convenient amount of file pointer. Before, there was a seek in our os to offset the reading start point, current read point and end point of SectionReader, offset offset, whence setting option 0: read start point, 1: current read point, 2: end point (not easy to use), other: Seek: invalid whence exception will be thrown
The copy code is as follows:
Import (
"fmt"
"io"
"os"
)
Func main () {
F, _: = os.Open ("test.txt")
Defer f.Close ()
Sr: = io.NewSectionReader (f, 2, 5)
P: = make ([] byte, 10)
Sr.Seek (1, 0) / / is equivalent to the starting address offset 1
N, err: = sr.Read (p)
If err! = nil {
Fmt.Println (err)
}
Does fmt.Println (string (p [: n])) / / lo w reach the previous ReadAt ()?
}
(5) func (s * SectionReader) Size () int64 returns the number of bytes that can be read, which is not affected by the offset pointer or the current read. Let's take a look at the code.
The copy code is as follows:
Import (
"fmt"
"io"
"os"
)
Func main () {
F, _: = os.Open ("test.txt")
Defer f.Close ()
Sr: = io.NewSectionReader (f, 2, 5)
Fmt.Println (sr.Size ()) / / 5
P: = make ([] byte, 10)
Sr.Seek (1, 0) / / is equivalent to the starting address offset 1
N, err: = sr.Read (p)
If err! = nil {
Fmt.Println (err)
}
Fmt.Println (string (p [: n])) / / lo w
Fmt.Println (sr.Size ()) / / 5
}
After reading the above, do you have any further understanding of the IO method of the GO language? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.