有没有办法检测围棋里的命令是不是管道的?

示例:

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)

Go相关问答推荐

Go 1.22 http mux:在同一路径上提供一个手柄和一个FS

读取JSON数据并在网页上显示

正确使用pgtype的方法

`docker system df` 与 `/system/df` (docker api 端点)

Go 中的sync.Cond 与 Wait 方法

Go:如何在不加载正文的情况下创建 http 代理通行证

如何从 Asterisk Manager Interface Event 获取活动呼叫数

在嵌套模板中使用变量,它也被定义为 go 模板中的变量?

设置指向空接口的指针

golang 中的可变参数函数

如何仅提取时间作为持续时间

使用 Golang 在字符串中循环重复数据

我相信我正确地在 sRGB 和线性 RGB 之间进行了转换,那么为什么深色的结果看起来更糟呢?

Go 泛型:自引用接口约束

Go AST:获取所有 struct

如何在golang中使用ozzo验证进行时间最大验证

如何在gorm中处理多个查询

在 Golang 中使用 OR 条件验证 struct 的两个字段

如何正确判断 io.Reader 是否为零?

Go Flag 用法 描述 包含 Word 值