I've just started to study Go
's filesystem operations. It seems like there are at least two ways to perform random file writes:
// 1. First set the offset, then write data
f.Seek(offset, whence)
f.Write(data)
// 2. Write by offset in one step
f.WriteAt(data, offset)
All of three functions (Seek
, Write
, WriteAt
) are implemented with the use of different syscalls: on Unix systems Write
is implemented via syscall.Write
and WriteAt
has syscall.Pwrite
inside.
Since Seek+Write
perform two syscalls, whereas WriteAt
requires only one syscall, should the second method be preferred for the sake of better performance?
seek()
+read()
and seek()
+ write()
are both a pair of sys-calls while pread()
and pwrite()
are single sys-calls. Less sys-calls - more efficiency.
You can definitly go for the WriteAt