Golang 重定向 fmt.Scanf 以从文件读取而不是 os.Stdin
Golang to redirect fmt.Scanf to read from file instead of os.Stdin
通常当我们在 Golang 中调用 fmt.Scanf()
时,程序会从标准输入流读取,即 os.stdin
我想通过 Golang 代码使 fmt.Scanf()
从文件流中读取 - 类似于 technique in Python here。
请注意,我们在命令行级别有管道解决方法;在这里我正在寻找 in-Golang 代码解决方案。
如果您知道如何到达那里,请分享。
p.s.
我想这需要我们
- 从文件流中读取并存储在字符串变量中
bytes
- 将
bytes
分配给 os.stdin
流
虽然我是一个 Golang 新手而且我的 google search 不是很有帮助所以我在这里问了。
正在将文件设置为 os.Stdin
如果这确实是您想要的:os.Stdin
是一个变量(类型为 *os.File
) which you can modify to your liking, you can assign a new value to it. Opening a file and assigning it to os.Stdin
, fmt.Scanf()
will read directly from that file. For details, see 。
这是一个完整的示例,它打开名为 a.txt
的文件,将其设置为 os.Stdin
,然后调用 fmt.Scanf()
从 [=13= 中读取 string
值](间接来自 a.txt
文件)。下面的代码还处理保存和恢复 os.Stdin
的原始值。这里不需要,只是作为一个例子,临时修改全局的东西,恢复一下也是个好习惯。
f, err := os.Open("a.txt")
if err != nil {
panic(err)
}
defer f.Close()
oldStdin := os.Stdin
defer func() { os.Stdin = oldStdin }()
os.Stdin = f
var s string
if _, err := fmt.Scanf("%s", &s); err != nil {
fmt.Printf("Error reading: %v", err)
}
fmt.Println("Read:", s)
请注意,您还可以将 os.Stdout
重定向到一个文件,例如:
f2, err := os.Create("out.txt")
if err != nil {
panic(err)
}
defer f2.Close()
oldStdout := os.Stdout
defer func() { os.Stdout = oldStdout }()
os.Stdout = f2
fmt.Printf("This will be written to the file: %s", f2.Name())
使用fmt.Fscanf()
一个更简单、更好的替代方法是使用 fmt.Fscanf()
which is analogue to fmt.Scanf()
, but here you can also pass an io.Reader
读取,os.File
实现 io.Reader
,因此您可以直接将文件传递给它。
将文件重定向到应用程序的标准输入
另一种选择是在您启动应用程序时将文件重定向到您的应用程序作为其标准输入。例如:
go run myapp.go < a.txt
此命令将启动您的应用,将 a.txt
文件的内容流式传输到您应用的标准输入。因此 fmt.Scanf()
将读取 a.txt
文件的内容。
通常当我们在 Golang 中调用 fmt.Scanf()
时,程序会从标准输入流读取,即 os.stdin
我想通过 Golang 代码使 fmt.Scanf()
从文件流中读取 - 类似于 technique in Python here。
请注意,我们在命令行级别有管道解决方法;在这里我正在寻找 in-Golang 代码解决方案。
如果您知道如何到达那里,请分享。
p.s.
我想这需要我们
- 从文件流中读取并存储在字符串变量中
bytes
- 将
bytes
分配给os.stdin
流
虽然我是一个 Golang 新手而且我的 google search 不是很有帮助所以我在这里问了。
正在将文件设置为 os.Stdin
如果这确实是您想要的:os.Stdin
是一个变量(类型为 *os.File
) which you can modify to your liking, you can assign a new value to it. Opening a file and assigning it to os.Stdin
, fmt.Scanf()
will read directly from that file. For details, see
这是一个完整的示例,它打开名为 a.txt
的文件,将其设置为 os.Stdin
,然后调用 fmt.Scanf()
从 [=13= 中读取 string
值](间接来自 a.txt
文件)。下面的代码还处理保存和恢复 os.Stdin
的原始值。这里不需要,只是作为一个例子,临时修改全局的东西,恢复一下也是个好习惯。
f, err := os.Open("a.txt")
if err != nil {
panic(err)
}
defer f.Close()
oldStdin := os.Stdin
defer func() { os.Stdin = oldStdin }()
os.Stdin = f
var s string
if _, err := fmt.Scanf("%s", &s); err != nil {
fmt.Printf("Error reading: %v", err)
}
fmt.Println("Read:", s)
请注意,您还可以将 os.Stdout
重定向到一个文件,例如:
f2, err := os.Create("out.txt")
if err != nil {
panic(err)
}
defer f2.Close()
oldStdout := os.Stdout
defer func() { os.Stdout = oldStdout }()
os.Stdout = f2
fmt.Printf("This will be written to the file: %s", f2.Name())
使用fmt.Fscanf()
一个更简单、更好的替代方法是使用 fmt.Fscanf()
which is analogue to fmt.Scanf()
, but here you can also pass an io.Reader
读取,os.File
实现 io.Reader
,因此您可以直接将文件传递给它。
将文件重定向到应用程序的标准输入
另一种选择是在您启动应用程序时将文件重定向到您的应用程序作为其标准输入。例如:
go run myapp.go < a.txt
此命令将启动您的应用,将 a.txt
文件的内容流式传输到您应用的标准输入。因此 fmt.Scanf()
将读取 a.txt
文件的内容。