To create an executable in Golang that hides the console window when run, you can utilize the -ldflags option during compilation.
The documentation suggests using the flag -Hwindowsgui when compiling:
go build -ldflags -Hwindowsgui filename.go
However, for newer versions of the compiler (1.1 ), the flag should be written as:
go build -ldflags -H=windowsgui filename.go
This flag hides the console window by compiling the executable with the Windows subsystem, which allows it to run without displaying a visible window.
To illustrate, let's create a simple program named invisible.go:
package main func main() { // Do something in the background }
You can compile this program using the following command:
go build -ldflags -H=windowsgui invisible.go
This will generate an executable named invisible.exe that can be run invisibly without opening a console window.
The above is the detailed content of How to Create a Background-Running Go Executable Without a Console Window?. For more information, please follow other related articles on the PHP Chinese website!