Created
August 23, 2025 18:42
-
-
Save veggiemonk/b39fae610d5d2e7e30ad33b2dd300276 to your computer and use it in GitHub Desktop.
test if data is coming in STDIN in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"golang.org/x/term" | |
) | |
// https://go.dev/play/p/iuo-AZ1oRPk | |
func main() { | |
fmt.Println("IsTerminal", term.IsTerminal(int(os.Stdin.Fd()))) | |
if isInputFromPipe() { | |
fmt.Println("stdin has data") | |
} else { | |
fmt.Println("stdin doesn't have data") | |
} | |
fmt.Println("os.Args", os.Args) | |
} | |
func isInputFromPipe() bool { | |
fi, _ := os.Stdin.Stat() | |
return (fi.Mode() & os.ModeCharDevice) == 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment