OpenFile contants meanings

f, err := os.OpenFile("text.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)

What is 0644 here?
How to understand O_WRONLY i.e. “Open the file write-only”? So it opens the file only for writing? And we can’t use the file for reading?

Check the os docs for more details, but in summary it is passing flags to OpenFile by or'ing them that state the file is to be created if it doesn’t exist (os.O_CREATE), it should be appended to if it does exist (os.O_APPEND, meaning not rewritten) and it’s opened as write only (os.O_WRONLY).

As for 0644, those are the file permissions. Check the Numeric notation section at https://en.wikipedia.org/wiki/File_system_permissions if you’re not familiar with numeric permissions.

3 Likes

This calculator, along with the Info and Code Examples tabs, might help:

http://permissions-calculator.org/

0644 is a very common setting. It allows read/write permissions for the owner of the file, and read-only for everyone else.

Search on “unix permissions” to find many pages that explain how it works. For example:

4 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.