英文:
How to hide console window of a Go program on Windows
问题
我尝试了各种方法来创建一个只显示MessageBox
或独立GUI窗口的Go程序。
如果我用C/C++来写,我只需要定义一个WinMain
,省略main
函数,然后就可以运行了。
在Go中,当我定义了一个main
函数时,似乎会自动创建一个控制台窗口,并且main
函数是必需的。
package main
func main() {
...
}
为了避免这个问题,我尝试了一个示例,它创建了一个WinMain函数。
func WinMain(wproc uintptr) {
hInstance := GetModuleHandle(nil)
...
}
英文:
I tried various ways of creating a Go program that only displays either a MessageBox
or a standalone GUI window.
If I were to write this in C / C++ I would just define a WinMain
, leave out the main and I would be good to go.
It seems to me that as soon as I define a main
function a console window is created automatically. And the main function is compulsory.
package main
func main() {
...
}
To avoid this I tried an example which creates a WinMain
func WinMain(wproc uintptr) {
hInstance := GetModuleHandle(nil)
...
}
But the effect is the same: an empty console window and a GUI window:
答案1
得分: 72
将-ldflags -H=windowsgui
添加到你的go build/install命令行中。你会发现控制台窗口消失了。
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论