No println output with **go build -ldflags="-H windowsgui"**?

Hello,
let say that I have a simple program containing “log.Println” or “fmt.Println” sentences. (I’m working on Windows 10).
if I build it simply with “go build”, I got console outputs when I launch the executable. was it in VSCode or if I launch it from a Cmdr window or DOS console, or simply by double clicking it in the explorer (the outputs are on the DOS console that automatically open).
now if I build it with “go build -ldflags="-H windowsgui”, I don’t have output on any console. nothing in VSCode, or if I launch the executable from a Cmdr window or a DOS console (and of course - and hopefully - no console is open if I just double click the executable).

is it the expected way of work…? why is there nothing returned and is there a way to direct the output to the caller…?
thanks
Fred

Without having access to Windows I’d say that, yes, this is expected.

On Windows, -H windowsgui writes a “GUI binary” instead of a “console binary.”

https://golang.org/cmd/link/#hdr-Command_Line

You can use the os package to write to a file as demonstrated on https://golang.org/pkg/os/#example_OpenFile_append.

Yeah, it’s expected, that’s how consoles work in Windows. You can also manage this on the fly from within your program.

func HideConsole() {
	getConsoleWindow := syscall.NewLazyDLL("kernel32.dll").NewProc("GetConsoleWindow")
	if getConsoleWindow.Find() != nil {
		return
	}

	showWindow := syscall.NewLazyDLL("user32.dll").NewProc("ShowWindow")
	if showWindow.Find() != nil {
		return
	}

	hwnd, _, _ := getConsoleWindow.Call()
	if hwnd == 0 {
		return
	}

	showWindow.Call(hwnd, 0)
}
1 Like

ok thanks.
now, even if “that’s how consoles work in Windows”, I’m nearly sure that I already coded Windows programs (with other languages) with a Windows GUI and no console, still able to log texts to the calling process…
for me GUI binary doesn’t mean that nothing can be send to the calling process, but just that no console window is open if I double click on a executable file. but maybe I’m wrong here. haven’t really done coding for a while…

not a problem actually, I will just “build” during development and add “-ldflags=”-H windowsgui" when development is finished. I don’t have other use than that for now, but was just surprised to see that behavior.
thanks…

[edit:] still something that is surely not the way it usually works on Windows; with another code example I totally messed, my app was crashing, so no GUI and… nothing… the panic message was not showing. I’m sure that on several other languages it’s not that way…
so, yes, the solution is easy, I will now always remove my default config wich was building with “-ldflags=”-H windowsgui" and add it only when all is finished…

Does the prog.exe > output.log write the output (>> for appending to file)?

nope…
an outpout.log file is created, but it’s empty if I run the file generated by “go build -ldflags=”-H windowsgui”.

if I try with only “go build”, the output.log I obtain contain the “fmt.Println” outputs, but not the “log.Println” outputs…
thanks

found more infos here.

and with a rapid test with that code, I got an “Hello!” output in VSCode terminal when build with "-ldflags=”-H windowsgui” :

package main

import "fmt"
import "os"
import "syscall"

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)
	return
}

func main() {
	ok := AttachConsole(ATTACH_PARENT_PROCESS)
	if ok {
		fmt.Println("Okay, attached")
	}
	hout, err1 := syscall.GetStdHandle(syscall.STD_OUTPUT_HANDLE)
	if err1 != nil { // nowhere to print the error
		os.Exit(2)
    }
	os.Stdout = os.NewFile(uintptr(hout), "/dev/stdout")
    fmt.Printf("Hello!\n")
   	var s string
	fmt.Scanln(&s)
	os.Exit(0)
}

for info if you are interested, I made a small package for that : guitocons

it’s probably not perfect, but working and small enough to be taken and adapted in your code if you want…

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.