问题描述
我了解CON
的名称与Linux中的/dev/tty
等效。
但是,当我使用写入该文件的程序时,它不打印任何内容,仅在Linux上有效。
我想我没有正确使用它。
重要提示::我不是在寻找像fmt.Println
这样的变通办法,我需要能够像在Linux(/dev/tty
)中一样打印到该文件。
这是程序:
package main
import (
"fmt"
"os"
"runtime"
)
func main() {
var ttyName string
if runtime.GOOS == "windows" {
fmt.Println("*** Using `con`")
ttyName = "con"
} else {
fmt.Println("*** Using `/dev/tty`")
ttyName = "/dev/tty"
}
f,_ := os.OpenFile(ttyName,os.O_WRONLY,0644)
fmt.Fprintln(f,"*** Stdout redirected")
}
程序在Linux上打印"*** Stdout redirected"
,但在Windows中不打印。
问题与ttyName
有关。
我使用con
作为名称,但似乎不起作用。
我也尝试过:"con:"
,"\\\\.\\con"
,"\\\\.\\con:"
,"\\\\?\\con"
和conout
。
如何使用它打印到控制台?
我从这个网站上收集了一些想法:
https://www.mkssoftware.com/docs/man5/dev_console.5.asp
解决方法
确保使用Windows命令提示符进行测试,因为CON
在第三方终端仿真器(例如IDE,Hyper.js等中的嵌入式仿真器)中可能无法正常工作。
您列出的选项应该起作用,大写的CON
(旧DOS名称)或\\.\CON
(UNC名称)是可以肯定的选择:
f,_ := os.OpenFile("CON",os.O_WRONLY,0644)
f,_ := os.OpenFile("\\\\.\\CON",0644)