Essential Go  Suggest an edit

Working with files and I/O

Go has a very robust support for file system handling.

A program that opens a file, reads the whole content and closes the file:

path := "main.go"
f, err := os.Open(path)
exitIfError(err)
defer f.Close()

d, err := ioutil.ReadAll(f)
exitIfError(err)

lines := bytes.Split(d, []byte{'\n'})
fmt.Printf("File %s has %d lines\n", path, len(lines))
File main.go has 32 lines

  ↑ ↓ to navigate     ↵ to select     Esc to close