NOTE

io

1. What it is I/O operations 2. What it contains 2.1. os 2.2. io 2.3. ioutil 2.4. bufio 3. References

Go1 min readhistorical

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 slice and returns the number of bytes read. When reading is complete it returns 0, io.EOF.
  • func (f *File) Write(b []byte) (n int, err error) writes a byte slice to a file and returns the number of bytes written. Usually len(b) == n unless an error occurs.
  • io and bufio depend on the os package.

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 Reader and Writer, which effectively abstract over objects such as os.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) error writes bytes to a file. If the file does not exist it is created; if it exists, it is replaced.

2.4. bufio

Uses the decorator pattern to enhance Reader and Writer, for example by adding buffering.

3. References

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub