Read file line by line in Go
In this article, we will discuss three different ways to read a file line by line in Go, using the bufio
, ioutil
, and os
packages.
Let us look at code examples and explanations for each method.
Using the bufio
package:
In this method, we use the bufio
package's NewScanner
function to read the file line by line. Then, the Scan
method of the Scanner
type reads the next line, and the Text
method returns the current line as a string. The loop continues until Scan
returns false, indicating that there are no more lines to read.
package main
import (
"bufio" // package for buffered I/O
"fmt"
"os" // package to interact with the underlying operating system
)
func main() {
// Open the file using the os package
file, err := os.Open("C:\\Personal\\Test\\readme.txt")
if err != nil {
fmt.Println(err)
return
}
defer file.Close() // Close the file when function returns
// NewScanner returns a new scanner to read from the file
scanner := bufio.NewScanner(file)
for scanner.Scan() {
fmt.Println(scanner.Text()) // Print the current line
}
if err := scanner.Err(); err != nil {
fmt.Println(err)
}
}
Output
Top Programming Languages
1) Golang
2) Python
3) C#
Using the ioutil
package:
In this method, we use the ioutil
package's ReadFile
function to read the entire file into a byte slice and then convert it to a string. Next, the strings.Split
function is used to split the string into lines, and a range loop is used to iterate over the lines.
package main
import (
"fmt"
"io/ioutil" // package for I/O operations
"strings"
)
func main() {
// Read the entire file into a byte slice
b, err := ioutil.ReadFile("C:\\Personal\\Test\\readme.txt")
if err != nil {
fmt.Println(err)
return
}
s := string(b) // Convert byte slice to string
for _, line := range strings.Split(s, "\n") {
fmt.Println(line) // Print the current line
}
}
Output
Top Programming Languages
1) Golang
2) Python
3) C#
Using the os
package:
In this method, we use the os
package's Open
function to open the file and the Read
method to read the file line by line. The Read
method returns the number of bytes read and an error, if any. The loop continues until the number of bytes read is zero, indicating that there are no more lines to read.
package main
import (
"fmt"
"os" // package to interact with the underlying operating system
)
func main() {
// Open file using the os package
file, err := os.Open("C:\\Personal\\Test\\readme.txt")
if err != nil {
fmt.Println(err)
return
}
defer file.Close() // Close the file when function returns
// Create a byte slice to hold the read data
byteSlice := make([]byte, 1024)
for {
// Read file using Read method of os package
n, err := file.Read(byteSlice)
if err != nil {
fmt.Println(err)
break
}
if n == 0 {
break // End of file
}
// Print the current line
fmt.Println(string(byteSlice[:n]))
}
}
Output
Top Programming Languages
1) Golang
2) Python
3) C#
Conclusion
In Go, there are several ways to read a file line by line. The method you choose will depend on your specific use case and the requirements of your program. The bufio
package provides a convenient Scanner
type that can be used to read a file line by line. The ioutil
package provides a simple ReadFile
function that can be used to read the entire file into a byte slice, which can then be converted to a string and split into lines. Finally, the os
package provides the Open
function to open a file and the Read
method to read the file line by line.