Go 入门很简单:Writer和Reader接口

2,928 阅读1分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第 16 天,点击查看活动详情

引言

我们学习其他语言编程时,会学到一个 io 包,这个包可以以流的方式高效处理数据,而不用考虑数据是什么,数据来自哪里,以及数据要发送到哪里的问题。

io 是一个 Golang 标准库包,它为围绕输入和输出的许多操作和用例定义了灵活的接口。

io 包参见:golang.org/pkg/io/

与 stdout 和 stdin 对应,Go 语言实现了 io.Writerio.Reader 两个接口。通过实现这两个接口,其他接口都可以使用 io 包提供的所有功能,也可以用于其他包里接收着两个接口的函数以及方法。

Go 还提供了名为 bufioioutil 的包,其中包含与使用这些接口相关的有用功能。

Writer 接口

io.Writer 接口是 Go 非常小的接口之一。它只有一种方法。写入方法。 Go 标准库中的许多包都使用 io.Writer 接口,它表示将字节切片写入数据流的能力。更一般地,允许您将数据写入实现 io.Writer 接口的东西。io.Writer 接口的声明如下:

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

这个接口声明了唯一一个方法 Writer,这个方法接收一个 byte 切片,并返回一个写入的字节数 n 和 error 错误值。

这里会有两个点需要注意:

  • Writer 从 p 字节切片的数据流写入 len(p) 字节的数据。这个方法返回从 p 里写出的字节数(0 <= n <= len(p) ),以及任何可能导致写入提起结束的错误。
  • Writer 在返回 n < len(p) 的时候,必须返回某个 nil 值的 error。Writer 绝不能改写切片里的数据,也不能临时修改。

现在来看一个例子,看在 Go 中将数据写入文件时如何使用 io.Writer

package main

import (
	"fmt"
	"os"
)

func main() {

	f, err := os.OpenFile("hello.txt", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
	if err != nil {
		panic(err)
	}
	defer f.Close()

	n, err := f.Write([]byte("Hello, My name is LeeLei!"))
	if err != nil {
		panic(err)
	}
	fmt.Printf("wrote %d bytes", n)
}

运行结果:wrote 25 bytes, 同时我们可以查看 hello.txt 文件内容:

image.png

os.File 的 Read 确实是同一个 io.Writer 接口的实现,用于将字节切片写入底层文件的数据流。这是 os.File.Write 的定义:

func (f *File) Write(b []byte) (n int, err error)

Reader 接口

然后来看一下 Reader 接口的声明:

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

Reader 是一个带有强抽象的小接口!那个抽象到底是什么? Read 方法的想法是它表示从某个源读取数据字节,以便我们可以在代码中使用这些字节。该来源可能是文件、相机、网络连接,或者只是一个普通的旧字符串。例如,如果我们从文件中读取数据,我们将使用的 io.Reader*os.File

io.Reader 接口声明了一个方法 Read,这个方法同样接受一个 byte 切片,返回两个值。第一个值是读入的字节数,第二个值是 error 错误值。

  • p []byte 是我们传递给 Read 方法的字节切片。 Reader 将从其数据源(如文件)读取的数据复制到该字节片。
  • 返回的 n int 告诉我们在这个 Read 调用中读取了多少字节。
  • 返回的 err error 是读取数据时可能发生的任何错误,例如到达文件末尾。

让我们用一个文件来尝试一下,看看我们是如何使用它的。复制此文本并将其保存到名为 hello.txt 的新目录中的文件中:

Hello, My name is LeeLei!

现在,让我们编写一些 Go 代码来使用 File 的 Read 方法读取它。复制此代码并将其保存到与 hello.txt 位于同一目录中的文件 main.go 中:

package main

import (
	"log"
	"os"
)

func main() {
	file, err := os.OpenFile("hello.txt", os.O_RDWR|os.O_CREATE|os.O_RDONLY, 0600)
	if err != nil {
		log.Fatalf("error opening hello.txt: %v", err)
	}
	defer file.Close()

	// Make a byte slice that's big enough to store a few words of the message
	// we're reading
	bytesRead := make([]byte, 33)

	// Now read some data, passing in our byte slice
	n, err := file.Read(bytesRead)
	if err != nil {
		log.Fatalf("error reading from hello.txt: %v", err)
	}

	// Take a look at the bytes we copied into the byte slice
	log.Printf("We read \"%s\" into bytesRead (%d bytes)",
		string(bytesRead), n)
}

运行该代码:go run main.go,然后可以看到如下输出:

$ go run main.go     
2022/04/18 22:56:29 We read "Hello, My name is LeeLei!" into bytesRead (25 bytes)

那么当我们调用 os.File.Read 时,Go 会从文件中读取数据,并将其复制到 bytesRead 中,由于没有发生错误,因此返回的错误为 nil。我们将 bytesRead 切片传递给 Read 函数,就像我们将杯子传递给汽水喷泉一样!

总结

本篇文章简单介绍了 Go 语言 io 包中的两个很实用的接口:Writer 和 Reader,分别给出了这两个接口的声明和解释,然后以一个简单的例子在代码中使用这两个小巧的接口,但是 Go 语言设计这两个接口的用法远远不止这些,也需要我们不断去探索其他功能,甚至学有余力去理解 Go 这样的设计的原理。

最后,感谢你的阅读,我们下一篇文章见!

参考链接: