Writing to a file in Golang across concurrent go routines
Go doesn't guarantee atomicity ... so I built a simple Go interface to do that


Okay, but is your implementation atomic? The only atomic operations guaranteed to be atomic in Go are through the sync/atomic package (or things like func (*Cond) Wait under sync).

If you need true atomic write, use the atomic package. However, using log is usually sufficient.



Your implementation looks like it's trying to be concurrent, not atomic. Rather than rolling your own concurrent writer interface, use the standard API.

You're right, fmt (&amp; os write functions, etc.) do not provide concurrency. However, the log package does.

You can see that they use mutex locks for the Output function, which is used by almost everything else.

This should perform nearly identical to your use case, because you open the file with O_APPEND, and log appends.

So open a file, and pass it to log.New().

For example:

package main

import (
    "log"
    "os"
)

func main() {
    f, err := os.OpenFile("testfile", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)

    if err != nil {
        log.Fatal(err)
    }

    logger := log.New(f, "", 0)
    logger.Output(2, "wow")
}