Visual applications with Go on Windows


Go is a great programming language, supporting more and more cross-platform libraries for the creation of visual applications - especially GTK 3.

The benefits are evident - providing a uniform graphical user interface (GUI) when developing native programs. I definitely appreciated GTK+'s flexibility and portability when creating MoonDeploy, my cross-platform deployment tool.

The setup on Linux is generally very simple - on Ubuntu, for example, you'll just have to install the gcc compiler system and the required C libraries via the apt-get package manager.

In this short article we will see how to configure a 64-bit Windows system in order to develop a visual application.

Installing msys2

We'll employ msys2, a brilliant development environment for Windows, including the MinGW-w64 compiler:

  1. Go to the msys2 website, download the installer having x86_64 in its name and run it
  2. Follow the wizard; we'll assume that msys is installed into the default directory C:\msys64
  3. As soon as the wizard has finished, run msys2's console (which should be suggested by the wizard itself)
  4. In the console, run:

    pacman --needed -Sy bash pacman pacman-mirrors msys2-runtime
  5. Close and restart the console
  6. Run:

    pacman -Su
  7. Restart the console
  8. To install MinGW-w64, run:

    pacman -S mingw-w64-x86_64-gcc
  9. To install pkg-config, run:

    pacman -S mingw64/mingw-w64-x86_64-pkg-config
  10. Add the following paths to your PATH environment variable:
  11. C:\msys64\mingw64\bin
  12. C:\msys64\usr\bin

Installing the GTK+ 3 libraries

Installing GTK+ 3 with msys2 requires a single instruction in msys2's console:

pacman -S mingw64/mingw-w64-x86_64-gtk3

Generally speaking, if you wish to look for a library whose you don't know the exact name, try running:

pacman -Ss text_to_search

Please, note: for 32-bit systems, the process is similar - just use a different installer and replace the x86_64 qualifier in package names.

Now, you can close msys2 - the installed tools will be available in DOS command windows as well.

Installing Go

  1. Install Go by downloading and running the installer from its website
  2. Set the GOROOT environment variable to Go's root directory
  3. Add Go's bin subdirectory to the PATH environment variable
  4. Create a directory that will be your Go path (for example, C:\GoPath)
  5. Assign such directory to the GOPATH environment variable

Installing GUI libraries for Go

This task usually requires Go's setup tool, go get; in particular, for gotk3, you'll need to run, in a DOS command window:

go get

If everything was installed correctly, the command should print no output, completing after a few minutes; otherwise, please double-check your msys2 setup.

Compiling a visual Go program

By default, on Windows, running GUI programs created with Go will show the DOS window as well, in the background.

To prevent this default behavior, you can compile the app by adding the following parameter to go build: -ldflags "-H windowsgui". For example:

go build -ldflags "-H windowsgui" SourceFile.go

On Linux, no additional arguments are required.

Further references