Hi, a total beginner here. I am wondering how to input a text file using command line?
Like in Python, I could type “python3 program.py < input-file.txt” in the command line to run program with the input text file, is it also possible in Go?
I was trying to use bufio to get some input and it works, but wondering how I can do it in the way I just wrote if it is possible in Go.
$ cat stdin.go
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
b := s.Bytes()
fmt.Println(string(b))
}
if err := s.Err(); err != nil {
fmt.Println(err)
}
}
$ cat stdin.file
This is data from a file.
.
$ go build stdin.go
$ ./stdin < stdin.file
This is from a file.
$ cat stdin.file | ./stdin
This is data from a file.