如果編譯golang源到一個Windows GUI可執行文件你用-ldflags -H=windowsgui
構建,每個exec.Command將產生一個新的控制檯窗口。
如果你沒有標誌建立,你會得到一個控制檯窗口和所有exec.Commands打印到那個。
我目前的解決方案是這樣建立不的標誌,即必須在程序啓動控制檯窗口,然後立即隱藏與此代碼在控制檯窗口中,在我的程序的啓動:
import "github.com/gonutz/ide/w32"
func hideConsole() {
console := w32.GetConsoleWindow()
if console == 0 {
return // no console attached
}
// If this application is the process that created the console window, then
// this program was not compiled with the -H=windowsgui flag and on start-up
// it created a console along with the main application window. In this case
// hide the console window.
// See
// http://stackoverflow.com/questions/9009333/how-to-check-if-the-program-is-run-from-a-console
_, consoleProcID := w32.GetWindowThreadProcessId(console)
if w32.GetCurrentProcessId() == consoleProcID {
w32.ShowWindowAsync(console, w32.SW_HIDE)
}
}
見this thread以獲取有關進程標識的詳細信息。
會發生什麼情況是所有exec.Commands現在將其輸出打印到隱藏的控制檯窗口而不是自己產生。
這裏的妥協是,當你啓動它時,你的程序會刷新一個控制檯窗口,但是隻有在它進入隱藏之前的一小段時間。
能否請您更精確地瞭解哪些提示您要隱藏? 你已經說過,正確的命令隱藏了兩個,Go命令行窗口和exec生成的窗口。我現在最好的猜測是你正在執行的命令會產生一個額外的窗口。 – Marco
這也是可能的,我將檢查哪個命令產生命令提示符(儘管很快消失) –
@ jm33_m0我剛剛嘗試過,在您的問題中的sysProcAttr方法,它是一個巨大的改進 - 窗口只是簡要地閃爍。你有沒有找到完全防止閃光的解決方案 – WebweaverD