How to read the whole file content into a string in Go?
Posted on In QAHow to read the whole file content into a string in Go?
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)
}