NOTE
io
Go I/O operations involve packages such as io and os. These notes organize their interfaces through cross-platform file access and error handling.
This is a historical learning note and may contain outdated or incomplete understanding.
1. What It Is
I/O operations.
2. What It Contains
2.1. os
- Provides cross-platform operating-system operations. On error it returns
PathError, from which detailed error information can be obtained. - Constants include modes such as read-only and write-only; variables include STDIN, STDOUT, and STDERR.
func Open(name string) (*File, error)opens a file.func OpenFile(name string, flag int, perm FileMode) (*File, error)opens the file if it exists, or creates it if it does not.func (f *File) Read(b []byte) (n int, err error)reads a file into a byte array and returns the number of bytes read. When reading is complete it returns0, io.EOF.func (f *File) Write(b []byte) (n int, err error)writes a byte array to a file and returns the number of bytes written. Usuallylen(b) == nunless an error occurs.ioandbufiodepend on theospackage.
2.2. io
The basic I/O package. It wraps lower-level operations such as those provided by os.
- It defines values such as
EOF. - It introduces the concepts of
ReaderandWriter, which wrapos.File.
2.3. ioutil
Commonly used I/O helper methods.
func ReadFile(filename string) ([]byte, error)reads a file and returns its bytes. It reads the whole file at once.func WriteFile(filename string, data []byte, perm os.FileMode) errorwrites bytes to a file. If the file does not exist it is created; if it exists, it is deleted and then recreated.
2.4. bufio
Uses the decorator pattern to enhance Reader and Writer, for example by adding buffering.
3. References
Drag to pan, scroll to zoom, and click a node to expand or collapse it. Large maps start with deeper levels collapsed; click nodes to expand them.
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub