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)
}
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.
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
d := []byte("data to write")
nWritten, err := f.Write(d)