Summary of IO method instances in GO language
- 2020-05-30 20:22:50
- OfStack
type PipeWriter
type PipeWriter struct {
// contains filtered or unexported fields
}
(1)func (w *PipeWriter) Close() error close the pipeline. The Read operation in progress at the time of closing will return EOF. If there is still unread data in the pipeline, it can be read normally afterwards
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)func (w *PipeWriter) CloseWithError(err error) error this function is much the same as CloseWithError in read. Close the pipeline, and the Read operation in process at the time of closing will return the exception passed in by parameters. If there is still unread data in the pipeline, it can still be read normally afterwards
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 hit write, which is to write the byte slice to the pipe, and return the number of bytes and error
import (
"fmt"
"io"
)
func main() {
r, w := io.Pipe()
go w.Write([]byte("hello widuu")) // Writing is []byte, Notice what the official documentation says is that the write pipe is blocked, 1 Until all the 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
type Reader interface {
Read(p []byte) (n int, err error)
}
(1)func LimitReader(r Reader, n int64) Reader, we talked about the Reader structure before, in fact, this is the first encapsulation of Reader, which limits the number of bytes it reads, in fact, it implements the io.LimitedReader {} structure
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 this function 1 is known to encapsulate multiple readers, similar to the above method, just encapsulate multiple, of course, also removed the reading limit, we code for you to test 1
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, let me write it down. The effect is only to write this time instead
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 This is 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 is an interesting method that reads data from r and then writes it to w, which has no internal buffer. Look at the code
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{}
type SectionReader struct {
// contains filtered or unexported fields
}
(1)func NewSectionReader(r ReaderAt, off int64, n int64) *SectionReader, you can see that, in fact, it is through this method to get io. SectionReader, the first parameter reader, the second parameter offset, the third parameter is how much to read
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)func (s *SectionReader) Read(p []byte) (n int, err error
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
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) 1: current read point, 2: end point (not useful), other: will throw Seek: invalid whence exception
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) // It's equivalent to the initial address offset 1
n, err := sr.Read(p)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(p[:n])) //lo w Does it reach the front ReadAt()
}
(5)func (s *SectionReader) Size() int64 returns the number of bytes that can be read, which is not affected by the offset pointer and is not affected by the current read. Let's look at the code in detail
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) // It's equivalent to the initial 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
}