In the field of computer and information technology, the term I/O refers to Input/Output (English: Input/Output), usually the Input and Output of data between memory (internal and external) or other peripheral devices. It is the communication between the information processing system and the external. Inputs are signals or data received by the system and outputs are signals or data sent from it.

There are many built-in libraries for I/O operations in Go, such as: IO library, OS library, ioutil library, Bufio library, Bytes library, Strings library, etc. Having so many built-in libraries is great, but which library should we choose for specific I/O scenarios?

io.Reader/Writer

Go uses IO.Reader and IO.Writer interfaces to abstract I/O.

type Reader interface {
	Read(p []byte) (n int, err error)
}

type Writer interface {
	Write(p []byte) (n int, err error)
}
Copy the code

The IO.Reader interface represents an entity to which the byte stream can be read, while IO.Writer drops an entity to which the byte stream can be written.

IO.Reader/Writer

  • Net.conn: Identifies the network connection.
  • Os. Stdin, os.Stdout, os.Stderr: Standard input, output, and errors.
  • Os. File: network, standard I/O, stream reading of files.
  • Strings. Reader: An implementation of IO.Reader abstracted from a string.
  • Bytes. Reader: []byte abstracts an implementation of IO.Reader.
  • Buffer: []byte abstracts IO.Reader and IO.Writer implementations.
  • Bufio. Reader/Writer: buffered streams read and write (such as row reads and writes).

The ioutil library contains many IO tool functions, and the encoding/base64 and encoding/binary libraries are implemented through IO.Reader and IO.Writer.

The relationship of these common implementations and libraries to IO.Reader and IO.Writer can be illustrated in the following figure.

Usage scenarios for each I/O library

IO library

The IO library belongs to the underlying interface definition library. Its main function is to define the basic I/O interface and basic constants, and explain the functions of these interfaces. The library typically only calls its constants and interface definitions when actually writing code to do I/O operations, such as using IO.EOF to determine if it has read or IO.Reader to declare variables.

// The error IO.EOF is returned after the byte stream is read
for {
	n, err := r.Read(buf)
	fmt.Println(n, err, buf[:n])
	if err == io.EOF {
		break}}Copy the code

OS library

The OS library primarily handles operating system operations and acts as a bridge between the Go program and the operating system. Creating files, opening and closing files, sockets, and so on are all tied to the operating system and are performed through OS libraries. This library is often used in conjunction with ioutil, Bufio, etc

Ioutil library

The ioutil library is a toolkit that provides many useful IO utility functions, such as ReadAll, ReadFile, WriteFile, and ReadDir. The only thing to note is that they are both one-time reads and one-time writes, so you need to pay attention to file sizes when using them, especially when reading data from a file into memory at once.

Read everything in the file

func readByFile(a) {
  data, err := ioutil.ReadFile( "./file/test.txt")
  iferr ! =nil {
    log.Fatal("err:", err)
    return
  }
  fmt.Println("data".string(data)) 
}
Copy the code

Write data to a file once

func writeFile(a) {
  err := ioutil.WriteFile("./file/write_test.txt"And []byte("hello world!"), 0644)
  iferr ! =nil {
    panic(err)
    return}}Copy the code

Bufio library

Bufio, which can be understood as an additional layer of caching on the basis of the IO library, provides many functions to read and write by line, from the IO library read and write by byte to read and write by line is still convenient for writing code.


func readBigFile(filePath string) error {
  f, err := os.Open(filePath)
  defer f.Close()

  iferr ! =nil {
    log.Fatal(err)
    return err
  }

  buf := bufio.NewReader(f)
  count := 0
  Print the first 100 lines in the loop
  for {
    count += 1
    line, err := buf.ReadString('\n')
    line = strings.TrimSpace(line)
    iferr ! =nil {
      return err
    }
    fmt.Println("line", line)

    if count > 100 {
      break}}return nil
}
Copy the code
  • ReadLine and ReadString methods: Buf.readline () and buf.readstring (“\n”) are both ReadLine by line, except that ReadLine reads []byte, which reads string directly, and eventually calls ReadSlice methods.
  • Bufio VS ioutil libraries: Both bufio and ioutil libraries provide the ability to read and write files. The only difference between them is that Bufio has an additional caching layer. This advantage mainly applies when reading large files.

Bytes and strings

The bytes and Strings libraries bytes.Reader and String. Reader, both of which implement the IO. Both provide NewReader methods to build Reader implementations directly from variables of type []byte or string.

r := strings.NewReader("abcde")
NewReader([]byte("abcde"))
buf := make([]byte.4)
for {
	n, err := r.Read(buf)
	fmt.Println(n, err, buf[:n])
	if err == io.EOF {
		break}}Copy the code

Another difference is that the Bytes library has Buffer functionality, while the Strings library does not.

var buf bytes.Buffer
fmt.Fprintf(&buf, "Size: %d MB.".85)
s := buf.String()) // s == "Size: 85 MB."
Copy the code

conclusion

The IO.Reader and IO.Writer interfaces can be simply understood as read source and write source. That is, as long as we implement the Read method in Reader, that thing can be Read as a source, and it can contain data that we can Read. The same is true of Writer.

The above is my Go language I/O operations often used in the Go language built-in library in the use of scenarios and the problems each library to solve some of the summary, I hope to help you clear your thinking, as a reference, in the development task when needed to correctly choose the right library to complete I/O operations. If there are any mistakes in the description of the article, welcome to comment on correction, also welcome to discuss the content of the article and put forward suggestions in the message.

That’s all for today’s article, please give me a thumbs up if you like my article, AND I will share what I have learned and seen and first-hand experience through technical articles every week. Thank you for your support. Wechat search concerns the public number “network management talk BI talk” every week to teach you an advanced knowledge.