Quick actions

cmd+k|ctrl+k

Navigation

Languages

Essential Go / Files and I/O

Snippet info

Language

Go

Visibility

public

Author

kkowalczyk

Created

2019-08-30T00:28:02Z

Updated

2019-08-30T00:28:02Z

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"log"
	"os"
)

func main() {
	path := "main.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))
}
INFO