If the code is performance sensitive, then you want to use a Buffer, and you‘ll 
need to serialize access. Otherwise each log line write will be a sys call - 
but depending on your use case that may be acceptable.

> On Jul 21, 2019, at 1:17 PM, Ian Lance Taylor <i...@golang.org> wrote:
> 
> On Sun, Jul 21, 2019 at 7:11 AM dev null <dev.null.dum...@gmail.com 
> <mailto:dev.null.dum...@gmail.com>> wrote:
>> 
>> Hi folks,
>> I have a program that has a function that appends data to the file - like a 
>> log appender appending to a file.
>> I open the file in append only mode
>> 
>> logFileWriter, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 
>> 0755)
>> 
>> and then there is a function to write to the file, which has simple code
>> 
>> logFileWriter.Write(logLine)
>> 
>> In the overall program, there are multiple go routines that will call this 
>> function to append log lines. My understanding is that there is need to 
>> protect this function using mutex because eventually at the syscall level 
>> they would be serialized anyway. its not possible that partial writes will 
>> happen (ignore crashes of the computer in the middle of writes etc. because 
>> mutex won't help anyway there).
> 
> I'm guessing that you meant to write "there is no need to protect".
> 
> To the best of my knowledge, that is correct.  Go will in effect be
> handing off synchronization to the kernel, and a Unix-like kernel
> should never interleave writes to a file opened in append mode.
> (Assuming your file is not mounted over NFS or some other varieties of
> networked file system.)  (I should add that I'm only asserting this
> for Unix-like kernels, I'm not sure about Windows.)
> 
> Ian
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golang-nuts+unsubscr...@googlegroups.com 
> <mailto:golang-nuts+unsubscr...@googlegroups.com>.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/CAOyqgcWNBvX96faRA1kfbfudQ7q1PkB6dGHLm%2B2Y%3DmP76dAV8w%40mail.gmail.com
>  
> <https://groups.google.com/d/msgid/golang-nuts/CAOyqgcWNBvX96faRA1kfbfudQ7q1PkB6dGHLm%2B2Y%3DmP76dAV8w%40mail.gmail.com>.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/E88C3EED-E2B9-4B31-96B0-F6E5DE7D0D24%40ix.netcom.com.

Reply via email to