当我以这样的模式打开文件时:
file, _ := os.OpenFile("/path/to/my/file", os.O_RDWR|os.O_APPEND, os.FileMode(0666))
file.Seek(start, os.SEEK_SET)
io.CopyN(file, resp.Body, length)
io.CopyN不尊重我寻求的位置。它似乎只是附加到文件的尾部。相反,如果我打开这样的文件:
file, _ := os.OpenFile("/path/to/my/file", os.O_RDWR, os.FileMode(0666))
file.Seek(start, os.SEEK_SET)
io.CopyN(file, resp.Body, length)
它按我的预期工作。 io.CopyN从我寻找的“开始”点写入文件。不确定这是一个功能还是一个错误?
答案 0 :(得分:11)
它绝对是一个功能(http://man7.org/linux/man-pages/man2/open.2.html),它由底层操作系统控制,而不是golang运行时。
O_APPEND
The file is opened in append mode. Before each write(2), the
file offset is positioned at the end of the file, as if with
lseek(2). O_APPEND may lead to corrupted files on NFS
filesystems if more than one process appends data to a file at
once. This is because NFS does not support appending to a
file, so the client kernel has to simulate it, which can't be
done without a race condition.