You can read an entire file into memory using the io/ioutil package.
packagemainimport ("io/ioutil""log")funcmain() { data, err := ioutil.ReadFile("example.txt")if err !=nil { log.Fatal(err) }// Use 'data' as needed}
2. Read File in Chunks
You can read a file in chunks by limiting the number of bytes to be read at a time. This approach is useful for large files to avoid loading everything into memory at once.
packagemainimport ("os""log")funcmain() { file, err := os.Open("example.txt")if err !=nil { log.Fatal(err) }defer file.Close() chunkSize :=1024// Adjust chunk size as needed buffer :=make([]byte, chunkSize)for { bytesRead, err := file.Read(buffer)if err !=nil {if err.Error() =="EOF" {break// End of file reached } log.Fatal(err) }// Use 'buffer[:bytesRead]' as needed }}
3. Read Line by Line
To read a file line by line, you can use the bufio package.