any file locks?

2,636 views
Skip to first unread message

Paul Stead

unread,
Dec 11, 2011, 11:50:12 PM12/11/11
to golan...@googlegroups.com
Hi everyone,

I've searched through packages and this mailing list and it appears to me that Go does not have any file locking mechanism?  I know about the mutex locking in the sync package, but I'm writing a system with multiple go processes, and assume that sync is only good between go routines in the same process space.  I've come across a situation where two of these processes need to manipulate the same files at times.  So I need some kind of locking mechanism to prevent them from corrupting the file with mutual writes and so on.  I'm using linux for these processes (ubuntu 11.10 32 bit right now, but I'll likely move them to fedora too).

Am I missing something or making incorrect assumptions?  Or are they planned?  Or is this just not a concept Go will support?

thanks,
Paul

Andrew Gerrand

unread,
Dec 12, 2011, 12:29:25 AM12/12/11
to golan...@googlegroups.com
The syscall package exposes the flock call on operating systems that support it:


Andrew

Paul Stead

unread,
Dec 12, 2011, 12:32:51 AM12/12/11
to golan...@googlegroups.com
Awesome.  Thanks.

Paul

you...@z505.com

unread,
Feb 10, 2016, 9:27:40 PM2/10/16
to golang-nuts

However, what to do when you have an OS that doesn't support Flock, write operating system specific code, or try to abstract Flock further to support other OS's if possible?

simon place

unread,
Feb 10, 2016, 10:00:05 PM2/10/16
to golang-nuts, you...@z505.com
what about a lock file.

jonathan...@gmail.com

unread,
Feb 11, 2016, 12:15:01 PM2/11/16
to golang-nuts
I would recommend revisiting if you need separate processes. But if you need them then you could stick to atomic operations like rename (from tmp file), mkdir, file create with the right flag etc. This would also allow your processes to play nice with a process out of your control.
Reply all
Reply to author
Forward
0 new messages