Files and I/O

suggest change

Go has a very robust support for working with files.

Here's a program that opens a file, reads its content and closes the file:

read file into lines.go

	path := "read file into lines.go"
	f, err := os.Open(path)
	if err != nil {
		log.Fatalf("os.Open() failed with %s\n", err)
	}
	defer f.Close()

	d, err := ioutil.ReadAll(f)
	if err != nil {
		log.Fatalf("ioutil.ReadAll() failed with '%s'\n", err)
	}

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

Output:

File 'read file into lines.go' has 29 lines

Reading files

Writing files

File operations

Directory operations

File path operations

I/O related interfaces

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents