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.

Code sample:

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. )
  6. func main() {
  7. os.Mkdir("astaxie", 0777)
  8. os.MkdirAll("astaxie/test1/test2", 0777)
  9. err := os.Remove("astaxie")
  10. if err != nil {
  11. fmt.Println(err)
  12. }
  13. os.RemoveAll("astaxie")
  14. }

Files

Create and open files

There are two functions for creating files:

  • func Create(name string) (file *File, err Error)

    Create a file with name and return a read-writable file object with permission 0666.

  • func NewFile(fd uintptr, name string) *File

    Create a file and return a file object.

There are also two functions to open files:

  • func Open(name string) (file *File, err Error)

    Opens a file called name with read-only access, calling OpenFile under the covers.

  • func OpenFile(name string, flag int, perm uint32) (file *File, err Error)

    Opens a file called name. flag is open mode like read-only, read-write, etc. perm are the file permissions.

Write files

Functions for writing files:

  • func (file *File) Write(b []byte) (n int, err Error)

    Write byte type content to a file.

  • func (file *File) WriteAt(b []byte, off int64) (n int, err Error)

    Write byte type content to a specific position of a file.

  • func (file *File) WriteString(s string) (ret int, err Error)

    Write a string to a file.

Code sample:

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. )
  6. func main() {
  7. userFile := "astaxie.txt"
  8. fout, err := os.Create(userFile)
  9. if err != nil {
  10. fmt.Println(userFile, err)
  11. return
  12. }
  13. defer fout.Close()
  14. for i := 0; i < 10; i++ {
  15. fout.WriteString("Just a test!\r\n")
  16. fout.Write([]byte("Just a test!\r\n"))
  17. }
  18. }

Read files

Functions for reading files:

  • func (file *File) Read(b []byte) (n int, err Error)

    Read data to b.

  • func (file *File) ReadAt(b []byte, off int64) (n int, err Error)

    Read data from position off to b.

Code sample:

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. )
  6. func main() {
  7. userFile := "asatxie.txt"
  8. fl, err := os.Open(userFile)
  9. if err != nil {
  10. fmt.Println(userFile, err)
  11. return
  12. }
  13. defer fl.Close()
  14. buf := make([]byte, 1024)
  15. for {
  16. n, _ := fl.Read(buf)
  17. if 0 == n {
  18. break
  19. }
  20. os.Stdout.Write(buf[:n])
  21. }
  22. }

Delete files

Go uses the same function for removing files and directories:

  • func Remove(name string) Error

    Remove a file or directory called name.( a name ending with / signifies that it’s a directory )