Create Text File
Create new folder named src. In src folder, create new folder named data. In data folder, create new text file named a.txt as below:
Line 1
Line 2
Line 3
Line 4
Application
In src folder, create new file named main.go as below:
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
file, err := os.Open("data/a.txt")
if err != nil {
fmt.Println(err)
} else {
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
fmt.Println(line)
}
}
file.Close()
}
Output
Open Terminal windows in Visual Studio Code and run command line: go run main.go
Line 1
Line 2
Line 3
Line 4