IO package
Introduction to the
IO package provides the most basic SYSTEM I/O encapsulation, which can be used to read, write, and copy files. Not thread-safe.
Mind mapping overview
Since mind maps are quite large, there are two ways to view them
- Browse mind maps online
- Download the mind map (open with the XMind tool)
function
Although there are a lot of methods and interfaces in the mind map above, we can use the following functions normally, which are also correctio
Methods in packages are further encapsulated.
copy
func Copy
func Copy(dst Writer, src Reader) (written int64, err error)
Copy the code
The dest parameter is the target file and the SRC parameter is the source file. You can copy SRC files to DST files. The return value written is the size of the copied file
Code examples:
The string stream is copied to the system output stream and the size of the copied data is printed
package main
import (
"fmt"
"io"
"os"
"strings"
)
func main(a) {
src:=strings.NewReader("This is the source file string stream.")
l,_:=io.Copy(os.Stdout,src)
fmt.Printf("\n Data size to be copied: %d",l)
}
Copy the code
Output:
This is the data size copied from the source file string stream: 27Copy the code
func CopyBuffer
func CopyBuffer(dst Writer, src Reader, buf []byte) (written int64, err error)
Copy the code
CopyBuffer is the same as Copy, except that CopyBuffer uses the supplied buffer and can customize the buffer size instead of allocating a temporary buffer. If buf is nil, a temporary buffer is allocated (func Copy calls this function and buF passes nil); If the length is 0, CopyBuffer will report an exception.
Code example: customize a buffer size of 100 bytes, then copy the string stream to the system output stream, and then print the copied data size
package main
import (
"fmt"
"io"
"os"
"strings"
)
func main(a) {
src:=strings.NewReader("This is the source file string stream.")
buf:=make([]byte.100)
l,_:=io.CopyBuffer(os.Stdout,src,buf)
fmt.Printf("\n Data size to be copied: %d",l)
}
Copy the code
Output:
This is the data size copied from the source file string stream: 27Copy the code
func CopyN
func CopyN(dst Writer, src Reader, n int64) (written int64, err error)
Copy the code
This function can define the size n of the stream to be copied. The underlying implementation is to call LimitReader and copy the result to Dest as SRC.
Code example: Copy 7 bytes of data to the system output stream, and then print the copied data size
package main
import (
"fmt"
"io"
"os"
"strings"
)
func main(a) {
src:=strings.NewReader("this is test!")
l,_:=io.CopyN(os.Stdout,src,7)
fmt.Printf("\n Data size to be copied: %d",l)
}
Copy the code
Output:
This is the size of the replicated data:7
Copy the code
read
func ReadAll
func ReadAll(r Reader) ([]byte, error)
Copy the code
Read all the bytes of r and return []byte
package main
import (
"fmt"
"io"
"strings"
)
func main(a) {
src:=strings.NewReader("this is test!")
b,_:=io.ReadAll(src)
fmt.Printf("Read string: %s",b)
}
Copy the code
Output:
Read string: This is test!Copy the code
func ReadAtLeast
func ReadAtLeast(r Reader, buf []byte, min int) (n int, err error)
Copy the code
- parameter
r
Represents what to readIO stream
- parameter
buf
Is a custom buffer, is also a data receiving area - parameter
min
Represents the minimum number of bytes to read
This function will read how much data depending on the size of the BUF you pass in. For example, if you pass in a buF of 4 bytes, this method will only read 4 bytes from r
Note: The custom buffer buf must be greater than the min read bytes. If the custom buffer is less than or equal to the min read bytes, an exception occurs
Code examples:
package main
import (
"fmt"
"io"
"strings"
)
func main(a) {
src:=strings.NewReader("this is test!")
buf:=make([]byte.4)
l,_:=io.ReadAtLeast(src,buf,2)
fmt.Printf("Read data: %s\n",buf)
fmt.Printf("Bytes read: %d",l)
}
Copy the code
Output:
Number of bytes read by this: 4Copy the code
func ReadFull
func ReadFull(r Reader, buf []byte) (n int, err error)
Copy the code
This function is the same as the ReadAtLeast function and is called internally, except that the min value of ReadAtLeast is the length of buF. So what this function does is it reads how much data depending on the size of the BUF you pass in.
write
func WriteString
func WriteString(w Writer, s string) (n int, err error)
Copy the code
- parameter
w
Is the target stream to write to - parameter
s
Is the string data to be written
This function writes the string S to stream W and returns the size of the written data
package main
import (
"fmt"
"io"
"os"
)
func main(a) {
l,_:=io.WriteString(os.Stdout,"go test!")
fmt.Printf("\n Size of data written: %d",l)
}
Copy the code
Output:
go test! Size of data to be written: 8Copy the code
Synchronous pipeline flow
func pipe
func Pipe(a) (*PipeReader, *PipeWriter)
Copy the code
This function provides a safe read stream function. Calling this function returns a write stream, a read stream. When the PipeReader is not read, the PipeWriter is always blocked. When the PipeReader is read, the Pipeeader becomes blocked, and data cannot be written to the pipe.
In other words, this is a thread-safe method, where at any given moment, you can either read or write data.
package main
import (
"fmt"
"io"
"os"
"time"
)
func main(a) {
r, w := io.Pipe()
go func(a) {
io.WriteString(w,"go pip test")
io.WriteString(w,"\ngo test2")
// Call the 'Close' method to indicate that the data is written and release the write lock
w.Close()
}()
fmt.Println("Ready to read data in 2 seconds.")
time.Sleep(2*time.Second)
io.Copy(os.Stdout, r)
}
Copy the code
Output:
Data is ready to be read in 2 seconds. Go PIP test go test2Copy the code