How to write a file in go

Oct 5, 2018 21:21 · 260 words · 2 minute read

The quickest way to write a file with some bytes is to use ioutil.WriteFile

package main

import (
	"fmt"
	"io/ioutil"
)

func main() {
	err := ioutil.WriteFile("somefile", []byte("foo\n"), 0644)
	if err != nil {
		fmt.Println("error while writing file")
	}
}

A more explicit way could be to use os.Create

package main

import (
	"fmt"
	"os"
)

func main() {
	f, err := os.Create("foo.dat")
	if err != nil {
		fmt.Println("error while creating file")
	}
	defer f.Close()

	l, err := f.Write([]byte("hello"))
	if err != nil {
		fmt.Println("error while creating file")
	}
	fmt.Printf("wrote %d bytes\n", l)
}

If you just want to write some string instead of byte array then just use WriteString

package main

import (
	"fmt"
	"os"
)

func main() {
	f, err := os.Create("foo.dat")
	if err != nil {
		fmt.Println("error while creating file")
	}
	defer f.Close()

	l, err := f.WriteString("foo bar\n")
	if err != nil {
		fmt.Println("error while creating file")
	}
	fmt.Printf("wrote %d bytes\n", l)
}

If multiple write operations are performed then make sure to use f.sync, to flush writes to stable storage.

Finally its worth mentioning a package buffio which helps with bufferred IO operations.

Many small writes can hurt performance, hence a common strategy is to buffer few writes in memory and then write a chunk to the disk as a single block.

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	f, err := os.Create("foo.dat")
	if err != nil {
		fmt.Println("error while creating file")
	}
	defer f.Close()

	w := bufio.NewWriter(f)
	l, err := w.WriteString("bufferred data\n")
	w.Flush() //write to storge.
	fmt.Printf("wrote %d bytes\n", l)
}