2014-05-19 16:56
浏览 615

使用-ldflags -H = windowsgui编译golang应用程序时,将输出打印到命令窗口

I have an application that usually runs silent in the background, so I compile it with

go build -ldflags -H=windowsgui <gofile>

To check the version at the command line, I wanted to pass a -V flag to the command line to get the string holding the version to be printed to the command prompt then have the application exit. I added the flag package and code. When I test it with

go run <gofile> -V

...it prints the version fine. When I compile the exe, it just exits, printing nothing. I suspect it's the compilation flag causing it to not access the console and sending my text into the bit bucket.

I've tried variations to print to stderr and stdout, using println and fprintf and os.stderr.write, but nothing appears from the compiled application. How should I try printing a string to the command prompt when compiled with those flags?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dongming6201 2014-05-19 18:05

    The problem is that when a process is created using an executable which has the "subsystem" variable in its PE header set to "Windows", the process has its three standard handles closed and it is not associated with any console—no matter if you run it from the console or not. (In fact, if you run an executable which has its subsystem set to "console" not from a console, a console is forcibly created for that process and the process is attached to it—you usually see it as a console window popping up all of a sudden.)

    Hence, to print anything to the console from a GUI process on Windows you have to explicitly connect that process to the console which is attached to its parent process (if it has one), like explained here for instance. To do this, you call the AttachConsole API function. With Go, this can be done using the syscall package:

    package main
    import (
    const (
        ATTACH_PARENT_PROCESS = ^uint32(0) // (DWORD)-1
    var (
        modkernel32 = syscall.NewLazyDLL("kernel32.dll")
        procAttachConsole = modkernel32.NewProc("AttachConsole")
    func AttachConsole(dwParentProcess uint32) (ok bool) {
        r0, _, _ := syscall.Syscall(procAttachConsole.Addr(), 1, uintptr(dwParentProcess), 0, 0)
        ok = bool(r0 != 0)
    func main() {
        ok := AttachConsole(ATTACH_PARENT_PROCESS)
        if ok {
            fmt.Println("Okay, attached")

    To be truly complete, when AttachConsole() fails, this code should probably take one of these two routes:

    • Call AllocConsole() to get its own console window created for it.

      It'd say this is pretty much useless for displaying version information as the process usually quits after printing it, and the resulting user experience will be a console window popping up and immediately disappearing; power users will get a hint that they should re-run the application from the console but mere mortals won't probably cope.

    • Post a GUI dialog displaying the same information.

      I think this is just what's needed: note that displaying help/usage messages in response to the user specifying some command-line argument is quite often mentally associated with the console, but this is not a dogma to follow: for instance, try running msiexec.exe /? at the console and see what happens.

    打赏 评论
  • douhuan1257 2016-09-12 04:04

    Answer above was helpful but alas it did not work for me out of the box. After some additional research I came to this code:

    // go build -ldflags -H=windowsgui
    package main
    import "fmt"
    import "os"
    import "syscall"
    func main() {
        modkernel32 := syscall.NewLazyDLL("kernel32.dll")
        procAllocConsole := modkernel32.NewProc("AllocConsole")
        r0, r1, err0 := syscall.Syscall(procAllocConsole.Addr(), 0, 0, 0, 0)
        if r0 == 0 { // Allocation failed, probably process already has a console
            fmt.Printf("Could not allocate console: %s. Check build flags..", err0)
        hout, err1 := syscall.GetStdHandle(syscall.STD_OUTPUT_HANDLE)
        hin, err2 := syscall.GetStdHandle(syscall.STD_INPUT_HANDLE)
        if err1 != nil || err2 != nil { // nowhere to print the error
        os.Stdout = os.NewFile(uintptr(hout), "/dev/stdout")
        os.Stdin = os.NewFile(uintptr(hin), "/dev/stdin")
    Result of console allocation: ")
    For Goodbye press Enter..", r0, r1, err0)
        var s string

    The key point: after allocating/attaching the console, there is need to get stdout handle, open file using this handle and assign it to os.Stdout variable. If you need stdin you have to repeat the same for stdin.

    打赏 评论
  • doushijia5684 2017-07-26 05:52

    You can get the desired behavior without using -H=windowsgui; you'd basically create a standard app (with its own console window), and hide it until the program exits.

    func Console(show bool) {
        var getWin = syscall.NewLazyDLL("kernel32.dll").NewProc("GetConsoleWindow")
        var showWin = syscall.NewLazyDLL("user32.dll").NewProc("ShowWindow")
        hwnd, _, _ := getWin.Call()
        if hwnd == 0 {
        if show {
           var SW_RESTORE uintptr = 9
           showWin.Call(hwnd, SW_RESTORE)
        } else {
           var SW_HIDE uintptr = 0
           showWin.Call(hwnd, SW_HIDE)

    And then use it like this:

    func main() {
        defer Console(true)
        fmt.Println("Hello World")
    打赏 评论
  • dream07769 2019-04-07 06:42

    One problem with the solutions already posted here is that they redirect all output to the console, so if I run ./myprogram >file, the redirection to file gets lost. I've written a new module, github.com/apenwarr/fixconsole, that avoids this problem. You can use it like this:

    import (
    func main() {
            err := fixconsole.FixConsoleIfNeeded()
            if err != nil {
                    fmt.Fatalf("FixConsoleOutput: %v
    ", err)
            os.Stdout.WriteString(fmt.Sprintf("Hello stdout
            os.Stderr.WriteString(fmt.Sprintf("Hello stderr
    打赏 评论

相关推荐 更多相似问题