https://www.bilibili.com/list/BV1gx4y1r7xb
1. 原生io包
- io包是Go語言標準庫中底層的I/O接口層,定義了通用的讀寫規則和錯誤處理邏輯。
- 每次讀寫都是直接調用底層系統 I/O,每次讀取1字節,系統調用次數多。
- 適用于小數據量、實時性要求高。
- io包中的核心接口。
type Reader interface { Read(p []byte) (n int, err error) }
type Writer interface { Write(p []byte) (n int, err error) }
type Closer interface { Close() error }
type Seeker interface { Seek(offset int64, whence int) (int64, error) }
2. 原生bufio包
- bufio在io包的基礎上實現了帶緩沖的I/O操作,目的是減少系統調用次數,提高性能。
- 批量讀取到緩沖區,再按需取用,減少系統調用次數。
- 適用于高頻讀寫、大文件操作或網絡通信。
- 完全遵循io包的接口規范,可以無縫替換原生的io包。