There are multiple ways to read data from the file.
The simplest way is to open a file and use a scanner to read data line by line. The sample code below does the same.
Other related posts:
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
readLineByLine("file_path")
}
func readLineByLine(filePath string) {
f, err := os.Open(filePath) //Open file for reading
if err != nil {
fmt.Println("Unable to open the file. Error: ", err)
return
}
defer f.Close() //Close file pointer after reading file.
line := bufio.NewScanner(f) //Scanner to read line by line
for line.Scan() {
fmt.Println(line.Text())
}
}
No comments:
Post a Comment