Essential Go  Edit on GitHub      File Issue

File 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 := "index.go"
f, err := os.Open(path)
defer f.Close()

d, err := ioutil.ReadAll(f)

lines := bytes.Split(d, []byte{'\n'})
fmt.Printf("File %s has %d lines\n", path, len(lines))
File index.go has 31 lines
  ↑ ↓ to navigate     ↵ to select     Esc to close