作者:时刻要有危机感01 | 来源:互联网 | 2023-02-04 08:11
有没有办法检测go中的命令是否有管道?
例:
cat test.txt | mygocommand #Piped, this is how it should be used
mygocommand # Not piped, this should be blocked
我正在读斯坦丁reader := bufio.NewReader(os.Stdin)
.
1> user513951..:
你可以使用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")
}
}
(改编自https://www.socketloop.com/tutorials/golang-check-if-os-stdin-input-data-is-piped-or-from-terminal)