7.5 Files

Files are essential objects on every single computer device. It won't come as any surprise to you that web applications also make heavy use of them. In this section, we're going to learn how to operate on files in Go.

Directories

In Go, most of the file operation functions are located in the os package. Here are some directory functions:

func Mkdir(name string, perm FileMode) error

Create a directory with name. perm is the directory permissions, i.e 0777.

func MkdirAll(path string, perm FileMode) error

Create multiple directories according to path, like astaxie/test1/test2.

func Remove(name string) error

Removes directory with name. Returns error if it's not a directory or not empty.

func RemoveAll(path string) error

Removes multiple directories according to path. Directories will not be deleted if path is a single path.