Tagged: golang-io

Golang: Read an entire file

You need to import the ‘io/ioutil’ package for this.

    fileBytes, err := ioutil.ReadFile("/path/to/your/file")

This will return a slice of bytes as the first return argument, not a string.

You can convert it easily:

    fileAsString := string(fileBytes)
golang golang-io

Golang: Buffered file reading

You can open a file using the ‘os’ package:

file, err := os.Open("/your/file/here")

You can also use the standard input, for example:

file := os.Stdin

If you want to read a line from either, you need the buffered reader from the package ‘bufio’:

bufferedReader := bufio.NewReader(file)

Now you can use the ReadString method, with a delimiter as the argument, to read a string from such:

str := bufferedReader.ReadString('\n')

This will give us a line from the file, including the delimiter we specified.

golang golang-io

Page 1 of 1