Essential Go File I/O  Edit on GitHub      File Issue

Writing files

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:

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 would need to use Windows API.

  ↑ ↓ to navigate     ↵ to select     Esc to close