Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create an executable from Golang that doesn't open a console window when run?

Tags:

console

go

I created an application that I want to run invisibly in the background (no console). How do I do this?

(This is for Windows, tested on Windows 7 Pro 64 bit)

like image 904
Bart Silverstrim Avatar asked Oct 08 '22 20:10

Bart Silverstrim


People also ask

How do I make a Go program executable?

From the command line in the hello directory, run the go build command to compile the code into an executable. From the command line in the hello directory, run the new hello executable to confirm that the code works.

Do you need go installed to run a go executable?

1 Answer. Show activity on this post. As long as any dependencies your program has are installed on the machine running the program, or your program is statically compiled, the user will be able to run your program without installing anything.

Where are go executables stored?

In the \Users\SomeUser\AppData\Local\Temp\ if you're on windows.


2 Answers

The documentation found online says I can compile with something along the lines of,

go build -ldflags -Hwindowsgui filename.go

But this gives an error: unknown flag -Hwindowsgui

With more recent (1.1?) versions of the compiler, this should work:

go build -ldflags -H=windowsgui filename.go

When I continued searching around I found a note that the official documentation should be updated soon, but in the meantime there are a lot of older-style example answers out there that error.

like image 63
Bart Silverstrim Avatar answered Oct 13 '22 08:10

Bart Silverstrim


Using Go Version 1.4.2

 go build -ldflags "-H windowsgui" 

From the Go docs:

go build [-o output] [-i] [build flags] [packages]

-ldflags 'flag list' arguments to pass on each 5l, 6l, or 8l linker invocation.

  • go build documentation
  • ldflags docs
like image 41
Shannon Matthews Avatar answered Oct 13 '22 09:10

Shannon Matthews