当前位置:网站首页>go io模块
go io模块
2022-06-11 21:39:00 【General_zy】
io包提供了对I/O原语的基本接口。本包的基本任务是包装这些原语已有的实现(如os包里的原语),使之成为共享的公共接口,这些公共接口抽象出了泛用的函数并附加了一些相关的原语的操作。
func Copy(dst Writer, src Reader) (written int64, err error)
将src的数据拷贝到dst,直到在src上到达EOF或发生错误。
对成功的调用,返回值err为nil而非EOF,因为Copy定义为从src读取直到EOF,它不会将读取到EOF视为应报告的错误。如果src实现了WriterTo接口,本函数会调用src.WriteTo(dst)进行拷贝;否则如果dst实现了ReaderFrom接口,本函数会调用dst.ReadFrom(src)进行拷贝。
func CopyN(dst Writer, src Reader, n int64) (written int64, err error)
从src拷贝n个字节数据到dst,直到在src上到达EOF或发生错误。
只有err为nil时,written才会等于n。如果dst实现了ReaderFrom接口,本函数很调用它实现拷贝。
func ReadAtLeast(r Reader, buf []byte, min int) (n int, err error)
ReadAtLeast从r至少读取min字节数据填充进buf。函数返回写入的字节数和错误(如果没有读取足够的字节)。只有没有读取到字节时才可能返回EOF;如果读取了有但不够的字节时遇到了EOF,函数会返回ErrUnexpectedEOF。 如果min比buf的长度还大,函数会返回ErrShortBuffer。只有返回值err为nil时,返回值n才会不小于min。
func ReadFull(r Reader, buf []byte) (n int, err error)
ReadFull从r精确地读取len(buf)字节数据填充进buf。函数返回写入的字节数和错误(如果没有读取足够的字节)。只有没有读取到字节时才可能返回EOF;如果读取了有但不够的字节时遇到了EOF,函数会返回ErrUnexpectedEOF。 只有返回值err为nil时,返回值n才会等于len(buf)。
func WriteString(w Writer, s string) (n int, err error)
WriteString函数将字符串s的内容写入w中。如果w已经实现了WriteString方法,函数会直接调用该方法。
ioutil
包ioutil实现了一些I/O实用程序函数。
func ReadAll(r io.Reader) ([]byte, error)
ReadAll从r读取数据直到EOF或遇到error,返回读取的数据和遇到的错误。成功的调用返回的err为nil而非EOF。因为本函数定义为读取r直到EOF,它不会将读取返回的EOF视为应报告的错误。
func ReadFile(filename string) ([]byte, error)
ReadFile 从filename指定的文件中读取数据并返回文件的内容。成功的调用返回的err为nil而非EOF。因为本函数定义为读取整个文件,它不会将读取返回的EOF视为应报告的错误。
func WriteFile(filename string, data []byte, perm os.FileMode) error
函数向filename指定的文件中写入数据。如果文件不存在将按给出的权限创建文件,否则在写入数据之前清空文件。
func ReadDir(dirname string) ([]os.FileInfo, error)
返回dirname指定的目录的目录信息的有序列表。
func TempDir(dir, prefix string) (name string, err error)
在dir目录里创建一个新的、使用prfix作为前缀的临时文件夹,并返回文件夹的路径。如果dir是空字符串,TempDir使用默认用于临时文件的目录(参见os.TempDir函数)。
func TempFile(dir, prefix string) (f *os.File, err error)
在dir目录下创建一个新的、使用prefix为前缀的临时文件,以读写模式打开该文件并返回os.File指针。如果dir是空字符串,TempFile使用默认用于临时文件的目录(参见os.TempDir函数)。
边栏推荐
- RPA超自动化 | 农耕记携手云扩加速财务智能化运营
- 判断大小端存储两种办法
- The network connection is normal, but Baidu web page can not be opened and displayed. You can't access this website solution
- LabVIEW控制Arduino实现红外测距(进阶篇—6)
- RPA+低代码为何是加速财务数字化转型之利器?
- [v2.1] automatic update system based on motion step API (repair bug, increase completion display, support disconnection reconnection and data compensation)
- 一步步把 SAP UI5 应用部署到 SAP BTP Kyma 运行环境中去
- BZOJ3189 : [Coci2011] Slika
- 2021 Niuke multi school 5 double strings
- How to create the simplest SAP kyma function
猜你喜欢
![[Part 13] source code analysis and application details of completabilefuture class [key]](/img/cf/87c60a1d46835f3f0dae9f44970de4.jpg)
[Part 13] source code analysis and application details of completabilefuture class [key]

类和对象(2)

LaTex实战笔记 3-宏包与控制命令

LeetCode-43-字符串相乘

RPA super automation | nongnongji and cloud expansion accelerate financial intelligent operation

Customer information management software

C语言实现八种排序 - 归并排序

Answer fans' questions | count the number and frequency of letters in the text

联调这夜,我把同事打了...

如何查看win系统的安装日期
随机推荐
快速排序的优化
LeetCode-43-字符串相乘
String copy function
ESP32C3 Arduino库使用方法
The upcoming launch of the industry's first retail digital innovation white paper unlocks the secret of full link digital success
Redis transaction
二分查找 - 学习
Leetcode-104- maximum depth of binary tree
The same efficiency tool for leading enterprises to promote smart finance. Let's have a quick look?
Redis basic data type (Zset) ordered collection
如何使用事物码 SAT 查找某个 SAPGUI 屏幕字段对应的后台存储数据库表的名称试读版
LeetCode-76-最小覆盖子串
bzoj3188 Upit
Database daily question --- day 9: salesperson
C语言实现八种排序 - 归并排序
领先企业推进智慧财务的同款效率工具,赶快了解一下?
Redis basic data type (set)
AC automata
B. Phoenix and Beauty
Add anti debugging function to game or code (application level)