Go语言中的Read操作的说明

本文介绍了Go语言中Reader接口的Read操作,包括Read方法的功能、参数解释和返回值含义。重点解析了当遇到错误或文件末尾时,Read可能的行为以及调用者如何正确处理返回值。Read调用可能会返回已读取的部分数据,而不仅仅是完整缓冲区的内容,并且在某些情况下,即使读取到文件末尾,也可能返回非nil的错误。
摘要由CSDN通过智能技术生成

Go语言中Reader接口定义了一个Read操作,实现了Reader接口的都有Read这个函数,接口的定义如下:

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

在实际使用的时候可能会对一个参数和这两个返回值有疑惑,什么时候返回什么值什么的,特别是类似于网络IO这种下面利用官网的文档说明一下。

官网文档

Reader is the interface that wraps the basic Read method.

Read reads up to len(p) bytes into p. It returns the number of bytes read (0 <= n <= len(p)) and any error encountered. Even if Read returns n < len(p), it may use all of p as scratch space during the call. If some data is available but not len(p) bytes, Read conventionally returns what is available instead of waiting for more.

When Read encounters an error or end-of-file con

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值