How to read the whole file content into a string in Go?

How to read the whole file content into a string in Go?

related to an answer for: How to process a file line by line in Go?
asked Jan 30 by anonymous

1 Answer

You can use the ReadFile() func from the io/ioutil package in Go to read the whole file content:

func ReadFile(filename string) ([]byte, error)

For example, read file filepath content and check the error:

if bytes, err := ioutil.ReadFile(filepath); err != nil {
  log.Fatal("Failed to read file: " + filepath)
}
answered Feb 12 by Eric Z Ma (43,500 points)

Please log in or register to answer this question.

Related questions

Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost

...