ioutil will be fully deprecated in Go 1.16. Although these APIs will continue to exist due to the compatibility guarantee, they are no longer recommended for use. So the question is: what should we use instead? Here are all the APIs in the ioutil package:
|
|
The corresponding replacement APIs in 1.16:
|
|
In summary, three key changes:
- Discard, NopCloser, ReadAll have been moved to the
iopackage - ReadDir, ReadFile, WriteFile have been moved to the
ospackage - TempDir, TempFile have been renamed to MkdirTemp, CreateTemp and moved to the
ospackage
ioutil 将在 Go 1.16 中被彻底弃用,虽然由于兼容性保障这些 API 还会继续存在,但不再被推荐使用了。那么问题来了,我们应该用什么?这是 ioutil 包所有的 API:
|
|
1.16 中取而代之的与之对应的 API:
|
|
总结起来就是三点:
- Discard, NopCloser, ReadAll 挪到了 io 包中
- ReadDir, ReadFile, WriteFile 挪到了 os 包中
- TempDir, TempFile 更名为了 MkdirTemp, CreateTemp 并挪到了 os 包中