I'm brand new to Go (as in within the last day) and am playing around with a simple program which processes data from stdin. What I wanted to do was to make it such that if no data were provided to stdin then the program would output a help screen and then exit. The problem I've run into is that the program seems to hang indefinitely when no data is provided via stdin. Here is a short example of the program and my intended usage:
package main
import (
"fmt"
"bufio"
"os"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
str := scanner.Text()
fmt.Println(str)
}
}
Running with input:
go run test.go < lines.txt
line1
line2
line3
Running with no input:
go run test.go
The second case where I do not provide input is what causes the program to hang. Reading through the docs it is not clear to me how I would be able to code the program to not wait for input indefinitely, but rather break if nothing is present on stdin.