NOTE
io
1. 是什么 I/O操作 2. 有什么 2.1. os 提供了跨平台的操作系统操作,错误了会返回PathError,可以从里面获取详细的错误信息 常量有只读、只写等模式,变量有STDIN、STDOUT、STDERR func Open(name string) ( File, error) 用于打开一个文件, func OpenFile(name string, flag int, perm FileMode) ( File, erro
这是历史学习笔记,可能存在过时或不完整的理解。
1. 是什么
I/O操作
2. 有什么
2.1. os
- 提供了跨平台的操作系统操作,错误了会返回PathError,可以从里面获取详细的错误信息
- 常量有只读、只写等模式,变量有STDIN、STDOUT、STDERR
func Open(name string) (*File, error)用于打开一个文件,func OpenFile(name string, flag int, perm FileMode) (*File, error)有则打开,无则创建func (f *File) Read(b []byte) (n int, err error)读取文件到字节数组中,返回读取的字节数,读完了返回了0,io.EOFfunc (f *File) Write(b []byte) (n int, err error)写字节数组到文件中,返回写入的字节数,一般len(b) == n除非出错了- io、bufio依赖于这个os包
2.2. io
基础IO包,封装了底层的low-level操作,比如os
- 变量有EOF
- 有了Reader和Writer的概念,其实就是封装了os的File
2.3. ioutil
io中常用的方法
func ReadFile(filename string) ([]byte, error)用于读取文件,返回byte。一次性读取整个文件func WriteFile(filename string, data []byte, perm os.FileMode) error把字节写入文件。文件不存在则创建,存在则先删除在创建
2.4. bufio
装饰器模式增强Reader和Writer,比如缓冲功能
讨论
使用 GitHub 账号参与讨论,评论会保存在 GitHub Issues 中。在 GitHub 查看