Read Write files in Go

Package ioutil implements I/O functions.

1
import "ioutil"

Read file:

1
config, error := ioutil.ReadFile("/etc/config")

Write to file:

1
error := ioutil.WriteFile("/etc/config", config, 0644)

Read direcrtory:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// ioutil.ReadDir returns slice of os.FileInfo
files, error := ioutil.ReadDir("/home/akshay")

if error != nil {
    // Handle error
}

for _, file := range files {
    // file.Size()
    // file.Name()
    // file.IsDir()
}

For more functions, read ioutil doc.