Golang 中的 IO 包详解:结构体类型介绍
创始人
2025-06-30 01:41:36
0

io.LimitedReader

// A LimitedReader reads from R but limits the amount of
// data returned to just N bytes. Each call to Read
// updates N to reflect the new amount remaining.
// Read returns EOF when N <= 0 or when the underlying R returns EOF.
type LimitedReader struct {
	R Reader // underlying reader
	N int64  // max bytes remaining
}

实现了 io.Reader 接口,并且进行了功能扩展。R 表示 io.Reader 对象,N 表示最多允许读取的字节数。简单示例如下所示:

package main

import (
	"bytes"
	"fmt"
	"io"
)

func main() {
	data := []byte("hello, world!")
	reader := io.LimitReader(bytes.NewReader(data), 5)
	buf := make([]byte, 10)
	n, err := reader.Read(buf)
	if err == nil {
		fmt.Printf("%s\n", buf[:n])
	} else {
		fmt.Printf("read error: %s\n", err)
	}
}

当读取的字节数超过限制时,LimitedReader 会自动终止读取并返回一个 io.EOF 错误,表示已经达到了总字节数的限制。

io.SectionReader

// SectionReader implements Read, Seek, and ReadAt on a section
// of an underlying ReaderAt.
type SectionReader struct {
	r     ReaderAt
	base  int64
	off   int64
	limit int64
}

实现了 io.Reader、io.ReaderAt 和 io.Seeker 接口的类型,用于在一个 Reader 中只读取某部分的数据。在使用 io.SectionReader 时,通常是将其作为参数传递给其他需要 ReaderAt 或 Seeker 接口的函数,并在该函数中使用 ReadAt 或 Seek 方法来访问数据。简单示例如下:

package main

import (
	"bytes"
	"fmt"
	"io"
)

func main() {
	data := []byte("hello, world!")
	reader := bytes.NewReader(data)
	sectionReader := io.NewSectionReader(reader, 0, 6)
	buf := make([]byte, 5)
	n, err := sectionReader.ReadAt(buf, 0)
	if err == nil {
		fmt.Printf("%s\n", buf[:n])
	} else {
		fmt.Printf("read error: %s\n", err)
	}
}

io.teeReader

type teeReader struct {
	r Reader
	w Writer
}

实现了 io.Reader 和 io.Writer 接口的类型,可以将输入流的内容复制到一个指定的输出流中。简单示例如下:

package main

import (
	"bytes"
	"fmt"
	"io"
)

func main() {
	data := []byte("hello, world!")
	buf1 := bytes.NewBuffer(nil)
	buf2 := bytes.NewBuffer(nil)
	reader := bytes.NewReader(data)
	tee := io.TeeReader(reader, io.MultiWriter(buf1, buf2))
	buf := make([]byte, 10)
	n, err := tee.Read(buf)
	if err == nil {
		fmt.Printf("%s\n", buf[:n])
		fmt.Printf("%s\n", buf1.Bytes())
		fmt.Printf("%s\n", buf2.Bytes())
	} else {
		fmt.Printf("read error: %s\n", err)
	}
}

io.PipeReader

// A PipeReader is the read half of a pipe.
type PipeReader struct {
	p *pipe
}

io.PipeReader 用于从 io.Pipe 中读取数据的类型。io.Pipe 实际上是一个管道,可以用于在同一个进程中的不同 goroutine 之间传输数据。PipeReader 实际上是通过 io.Pipe 返回的读取端实例。使用起来非常简单,可以通过 io.Pipe 函数创建一个 Pipe 实例,io.Pipe 函数返回的是两个值分别是 io.PipeReader 和 io.PipeWriter 类型的指针,前者用于从管道中读取数据,后者用于向管道中写入数据。简单示例如下:

package main

import (
	"bufio"
	"fmt"
	"io"
)

func main() {
	pr, pw := io.Pipe()
	go func() {
		pw.Write([]byte("hello, world!"))
		pw.Close()
	}()
	br := bufio.NewReader(pr)
	line, isPrefix, err := br.ReadLine()
	fmt.Println(line, isPrefix, err)
}

io.PipeWriter

// A PipeWriter is the write half of a pipe.
type PipeWriter struct {
	p *pipe
}

io.PipeWriter 是用于向 io.Pipe 中写入数据的类型。io.Pipe 实际上是一个管道,可以用于在同一个进程中的不同 goroutine 之间传输数据。PipeWriter 实际上是通过 io.Pipe 返回的写入端实例。io.Pipe 使用起来非常简单,可以通过 io.Pipe 函数创建一个 Pipe 实例,io.Pipe 函数返回的是两个值,分别是 io.PipeReader 和 io.PipeWriter 类型的指针,前者用于从管道中读取数据,后者用于向管道中写入数据。

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...