有没有办法检测围棋里的命令是不是管道的?
示例:
cat test.txt | mygocommand #Piped, this is how it should be used
mygocommand # Not piped, this should be blocked
我在读标准reader := bufio.NewReader(os.Stdin)
.
有没有办法检测围棋里的命令是不是管道的?
示例:
cat test.txt | mygocommand #Piped, this is how it should be used
mygocommand # Not piped, this should be blocked
我在读标准reader := bufio.NewReader(os.Stdin)
.
您可以使用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)