英文:
How to check os.stdin is available before prompting for something with text "Enter input"
问题
我需要从os.stdin中获取一些输入。在打印类似"请输入文本"的语句之前,我该如何检查是否可以读取输入。如果stdin不可用,它会在"请输入文本"之后打印"bad file descriptor"。如何避免这种情况?
package main
import (
"bufio"
"fmt"
"io"
"os"
)
func main() {
consSource := NewConsoleAccessTokenSource("www.google.co.in", os.Stdin)
fmt.Print("Token: ")
consSource.scanner.Scan()
err := consSource.scanner.Err()
if err != nil {
fmt.Print(err)
}
fmt.Print(consSource.scanner.Text())
}
func NewConsoleAccessTokenSource(websiteUrl string, reader io.Reader) *ConsoleAccessTokenSource {
s := &ConsoleAccessTokenSource{}
s.WebsiteUrl = websiteUrl
s.scanner = bufio.NewScanner(reader)
return s
}
type ConsoleAccessTokenSource struct {
WebsiteUrl string
scanner *bufio.Scanner
}
这是我尝试的内容。当我使用"nohup可执行文件"运行此程序时,它会显示"bad file descriptor"。
英文:
I have to promt for some input from os.stdin.How can I check this is available for reading before printing statements like "Enter your text" and then read from input.If stdin is not available it is printing bad file descriptor after "Enter your text". how to avoid this?
package main
import (
"bufio"
"os"
"io"
"fmt"
)
func main(){
consSource := NewConsoleAccessTokenSource("www.google.co.in", os.Stdin)
fmt.Print("Token: ")
consSource.scanner.Scan()
err := consSource.scanner.Err()
if err != nil {
fmt.Print(err)
}
fmt.Print(consSource.scanner.Text())
}
func NewConsoleAccessTokenSource(websiteUrl string, reader io.Reader) *ConsoleAccessTokenSource {
s := &ConsoleAccessTokenSource{}
s.WebsiteUrl = websiteUrl
s.scanner = bufio.NewScanner(reader)
return s
}
type ConsoleAccessTokenSource struct {
WebsiteUrl string
scanner *bufio.Scanner
}
This is what I am trying to do .when I run this program using "nohup executable"
it is giving bad file descriptor.
答案1
得分: 3
os.Stdin
是os
包的一个导出变量,它的类型是*os.File
。
你可以调用File.Stat()
来查看它是否可用,并获取关于它的其他信息(例如,它是否被管道传输或其来源是否是终端):
if _, err := os.Stdin.Stat(); err != nil {
fmt.Println("Stdin not available:", err)
} else {
fmt.Println("Stdin available.")
}
让我们看一个不可用的示例。如果我们首先关闭它,例如使用File.Close()
方法:
fmt.Println("Closing:", os.Stdin.Close())
if _, err := os.Stdin.Stat(); err != nil {
fmt.Println("Stdin not available:", err)
} else {
fmt.Println("Stdin available.")
}
输出结果(在Go Playground上尝试):
Stdin available.
Closing: <nil>
Stdin not available: stat /dev/stdin: Bad file number
还可以查看相关问题:https://stackoverflow.com/questions/22744443/check-if-there-is-something-to-read-on-stdin-in-golang
英文:
os.Stdin
is an exported variable of the os
package, it is of type *os.File
.
You may call File.Stat()
on it to see if it's available and also to get additional info about it (e.g. if it is being piped or its source is a terminal):
if _, err := os.Stdin.Stat(); err != nil {
fmt.Println("Stdin not available:", err)
} else {
fmt.Println("Stdin available.")
}
Let's see an example when it's not available. It won't be if we close it first e.g. with the File.Close()
method:
fmt.Println("Closing:", os.Stdin.Close())
if _, err := os.Stdin.Stat(); err != nil {
fmt.Println("Stdin not available:", err)
} else {
fmt.Println("Stdin available.")
}
Output (try it on the Go Playground):
Stdin available.
Closing: <nil>
Stdin not available: stat /dev/stdin: Bad file number
Also check related question: https://stackoverflow.com/questions/22744443/check-if-there-is-something-to-read-on-stdin-in-golang
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论