Writing files

suggest change

Write the whole file

The simplest way to write data to a file:

d := []byte("content of the file")
err := ioutil.WriteFile("foo.txt", d, 0644)
if err != nil {
    log.Fatalf("ioutil.WriteFile failed with '%s'\n", err)
}

Open file for writing

f, err := os.Create("foo.txt")
if err != nil {
    log.Fatalf("os.Open failed with '%s'\n", err)
}

Create returns *os.File which implements io.Writer and io.Closer interfaces.

If file doesn’t exist, it’ll be created.

If file does exist, it’ll be truncated.

You should always close files to avoid leaking file descriptors.

Be aware that Close on a file can return an error so for a robust code you should check for errors from Close.

Writes can be buffered and Close might need to flush remaining cached bytes to disk. That might fail and return an error.

Open file for appending

f, err := os.OpenFile(filePath, os.O_WRONLY | os.O_APPEND | os.O_CREATE, 0666)
if err != nil {
    log.Fatalf("os.Open failed with '%s'\n", err)
}

Second argument to os.OpenFile is a flag that determines the exact mode for opening the file.

When you open for reading, use os.Open.

When you open for writing to a new file, use os.Create.

When you open for appending to existing file, use os.OpenFile with the following flags: * os.O_WRONLY. Could also be os.RDWR if we also to both read and write * os.O_APPEND means that if file exists, we’ll append * os.O_CREATE means that if file doesn’t exist, we’ll create it. Without this flag opening non-existing file would fail

Write to a file

d := []byte("data to write")
nWritten, err := f.Write(d)

File permissions when creating files

When you create a new file with os.Create or os.OpenFile, you need to provide file permissions for the new file.

For most cases 0644 is a good choice.

Those are Unix style permissions in octal format.

Let’s deconstruct parts of 0644

Those permissions are native to Unix and Mac OS but they map loosely on Windows.

To manage Windows permissions you need to use Windows API.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents