I understood that the CON
name is equivalent to /dev/tty
in Linux.
But when I used a program that write to this file, it doesn't print anything, it works only on Linux.
I think I am not using it correctly.
IMPORTANT: I am not looking for workarounds like fmt.Println
, I need to be able to print to this file, like I did in Linux (/dev/tty
).
This is the program:
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")
}
The program prints "*** Stdout redirected"
in Linux but not in Windows.
The problem is related to the ttyName
.
I used con
as the name but it seems that it doesn't work.
I also tried: "con:"
, "\\\\.\\con"
, "\\\\.\\con:"
, "\\\\?\\con"
, and conout
.
How can I use it to print to the console?
I took some of the ideas from this website:
https://www.mkssoftware.com/docs/man5/dev_console.5.asp
Make sure you test this with Windows Command Prompt as CON
might not work as expected with 3rd party terminal emulators (like embedded ones in IDE, Hyper.js, etc.).
Options you listed should work, CON
(legacy DOS name) or \\.\CON
(UNC name) in uppercase are safe bet:
f, _ := os.OpenFile("CON", os.O_WRONLY, 0644)
f, _ := os.OpenFile("\\\\.\\CON", os.O_WRONLY, 0644)