How to hide command prompt window when using Exec in Golang?


say i have the following code, using syscall to hide command line window

process := exec.Command(name, args...)
process.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
err := process.Start()
if err != nil {

but when i compiled it and tried to run it in Windows, command line window showed up again

what can i do to prevent command line window from appearing?

PS i already know how to compile golang source into a Windows GUI executable using go build -ldflags -H=windowsgui, but doing so only ensures the program itself doesn’t bring up a command line window, Exec will show those windows anyway


There is a better solution, which can run exec.Command() without spawn a visible window, ( ͡° ͜ʖ ͡°).

Here is my code:

Firstly import "syscall"

cmd_path := "C:\\Windows\\system32\\cmd.exe"
cmd_instance := exec.Command(cmd_path, "/c", "notepad")
cmd_instance.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
cmd_output, err := cmd_instance.Output()


Answered By – Yu Chen

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published