有没有办法检测go中的命令是否有管道?
例:
cat test.txt | mygocommand #Piped, this is how it should be used
mygocommand # Not piped, this should be blocked
Run Code Online (Sandbox Code Playgroud)
我正在读斯坦丁reader := bufio.NewReader(os.Stdin).
use*_*951 11
你可以使用os.Stdin.Stat().
package main
import (
"fmt"
"os"
)
func main() {
fi, _ := os.Stdin.Stat()
if (fi.Mode() & os.ModeCharDevice) == 0 {
fmt.Println("data is from pipe")
} else {
fmt.Println("data is from terminal")
}
}
Run Code Online (Sandbox Code Playgroud)
(改编自https://www.socketloop.com/tutorials/golang-check-if-os-stdin-input-data-is-piped-or-from-terminal)