os.WriteFile()
Writing into Files
WriteFile writes data to the named file, creating it if necessary. If the file does not exist, WriteFile creates it with permissions perm (before umask); otherwise WriteFile truncates it before writing, without changing permissions. Since WriteFile requires multiple system calls to complete, a failure mid-operation can leave the file in a partially written state.
Paramteres: file, []byte and permission.
Example usage:
os.WriteFile("balance.txt", []byte(balanceText), 0644)Don't forget to create a new variable with Sprintf to later convert your variable into a []byte.
More information about the given 0644 permission:
The "0644" is a representation of file permissions in octal notation, commonly used in Unix and Unix-like operating systems. Let's break it down:
The leading "0" indicates that this is an octal number.
The remaining three digits (644) represent permissions for three different categories of users:
The first digit (6): Owner permissions
The second digit (4): Group permissions
The third digit (4): Others (everyone else) permissions
Each digit is the sum of the following values:
4: Read permission
2: Write permission
1: Execute permission
So, 0644 translates to:
Owner (6): Read (4) + Write (2) = 6
Group (4): Read (4)
Others (4): Read (4)
In symbolic notation, this would be written as "-rw-r--r--", where:
The first "-" represents the file type (in this case, a regular file)
"rw-" represents owner permissions (read and write)
"r--" represents group permissions (read only)
"r--" represents others permissions (read only)
This permission set is common for regular files that should be readable by everyone but only writable by the owner.
Last updated