You can see that they use mutex locks for the Output function, which is used by almost everything else. You're right, fmt (& os write functions, etc.) do not provide concurrency. Rather than rolling your own concurrent writer interface, use the standard API. Your implementation looks like it's trying to be concurrent, not atomic. However, using log is usually sufficient. If you need true atomic write, use the atomic package. 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). so I built a simple Go interface to do that Would there be an advantage to mutex locks here using the sync package? I could for instance make it conform to the io.Writer interface, but I'm not sure what benefits there are of that? NewFileLogger returns a new FileLogger FileLogger defines the methods to log to fileįmt.Println("Error writing to file: ", err.Error()) I'm not super proficient with Go, but I'd like to understand how this code can be improved with best practices and on potential issues that may arise when using it. Although I didn't see any interleaved writes (which I guess could be due to writev(2)), I didn't want to risk it so I built a a simple Go interface to do that. I've been reading around how golang writes to a file, and this stack overflow question and this reddit question highlights the fact Go doesn't gurantee atomicity when writing to a file system.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |