Your First GTK App with Go and VSCodium

GTK+3 Go App with VSCodium and Manjaro Xfce on Pinebook Pro

GTK+3 Go App with VSCodium and Manjaro Xfce on Pinebook Pro

Creating desktop apps on Linux doesn't have to be hard... Let's build GTK+3 apps in Go with the gotk3 library!

Read on to find out how we install the GTK+3 Go tools to build desktop apps on Manjaro, Arch Linux, Ubuntu and Debian.

The steps have been tested on...

  1. Pinebook Pro with Manjaro Xfce (other flavours should work fine)

  2. Pinebook Pro with Debian Mate (consider installing Manjaro Xfce for much better performance)

  3. Raspberry Pi 4 with Manjaro Xfce 64-bit (other flavours should work fine)

  4. Raspberry Pi 4 with Ubuntu Desktop 20.04 64-bit

They will probably work on Intel x64 distros of Linux too.

(Sorry, Raspberry Pi with Raspbian refuses to install the GTK tools... So please use Manjaro or Ubuntu)

GTK+3 Go App with Ubuntu Desktop 20.04 64-bit on Raspberry Pi 4

GTK+3 Go App with Ubuntu Desktop 20.04 64-bit on Raspberry Pi 4 with 4 GB RAM

1 Coding GTK with Go

We'll be creating this simple desktop GUI app...

GTK3 Go App

With this Go code that calls the gotk3 library...

func newStackFull() gtk.IWidget {
	// get a stack and its switcher.
	stack, err := gtk.StackNew()
	if err != nil {
		log.Fatal("Unable to get text:", err)
	}

	sw, err := gtk.StackSwitcherNew()
	if err != nil {
		log.Fatal("Unable to get text:", err)
	}
	sw.SetStack(stack)

	// Fill the stack with 3 pages.
	boxText1 := newBoxText("Hello there!")
	boxRadio := newBoxRadio("choice 1", "choice 2", "choice 3", "choice 4")
	boxText2 := newBoxText("third page")

	stack.AddTitled(boxText1, "key1", "first page")
	stack.AddTitled(boxRadio, "key2", "second page")
	stack.AddTitled(boxText2, "key3", "third page")

	// You can use icons for a switcher page (the page title will be visible as tooltip).
	stack.ChildSetProperty(boxRadio, "icon-name", "list-add")

	// Pack in a box.
	box := setup_box(gtk.ORIENTATION_VERTICAL)
	box.PackStart(sw, false, false, 0)
	box.PackStart(stack, true, true, 0)
	return box
}

See the rest of the code here: gtk-examples/stack/stack.go

Why use Go to code GTK apps?

  1. Go looks so neat and tidy for writing GUI apps with GTK!

  2. Go compiles to machine code so it's fast (More details)

  3. Go supports Garbage Collection, so no worries about scary C pointers

In the code above, why are type names omitted when we declare variables?

Because Go is Static Typed and it inferences the types of variables based on their usage.

No more runtime type errors! (The ones we see in JavaScript)

2 Install Build Tools

1️⃣ For Manjaro and Arch Linux...

At the command prompt enter...

sudo pacman -S gtk3 cairo glib2 go

Proceed to the next section "Install VSCodium"

2️⃣ For Ubuntu and Debian...

At the command prompt enter...

sudo apt install libgtk-3-dev libcairo2-dev libglib2.0-dev

Follow the instructions here to install the Go build tools:

golang.org/doc/install

VSCodium on Manjaro Xfce installed via Arch User Repository

VSCodium on Manjaro Xfce installed via Arch User Repository

3 Install VSCodium

We'll be installing VSCodium, the community build of VSCode that doesn't have Microsoft branding and telemetry. VSCodium has better support for Arm Linux than VSCode.

Note that VSCodium uses its own Extensions Marketplace, so that it doesn't violate Microsoft's terms of use. VSCodium Marketplace Extensions may be slightly different from the VSCode Marketplace.

1️⃣ For Manjaro and Arch Linux...

Enable Arch User Repository (AUR) in the Arch Package Manager pamac-manager...

"How to Use AUR on Arch and Manjaro Linux?"

In the Package Manager, search for vscodium-bin and install it.

2️⃣ For Ubuntu and Debian...

Follow the instructions here to install VSCodium:

github.com/VSCodium/vscodium

4 Download Source Code For Our App

Let's download the gotk3 sample code...

  1. Launch VSCodium, click View → Command Palette

  2. Enter Git Clone

  3. Enter https://github.com/gotk3/gotk3-examples

  4. Select a folder to download the source code

  5. When prompted to open the cloned repository, click Open

  6. In the Explorer → Workspace pane at left, browse to gtk-examples → stack

  7. Double-click on stack.go. This is the Go app that we shall be building...

Source code of our GTK3 Go app in VSCodium

5 Build And Run Our App

  1. In VSCodium, click Terminal → New Terminal

  2. At the Terminal prompt, enter...

    cd gtk-examples/stack
    go get github.com/gotk3/gotk3/gtk
    go build stack.go
    ls -l
    

    The go get command may take 10 minutes or longer to build the gotk3 library... Be patient! This only needs to be done once.

    The ls -l command should show the compiled Go executable named stack...

    Our compiled GTK3 Go App

  3. At the Terminal prompt, enter...

    ./stack
    

    This launches our newly-built GTK+3 Go app...

    Our GTK3 Go app

And that's how we build GTK apps with Go!

GTK+3 Go App with VSCodium and Manjaro Xfce 64-bit on Raspberry Pi 4 with 4 GB RAM

GTK+3 Go App with VSCodium and Manjaro Xfce 64-bit on Raspberry Pi 4 with 4 GB RAM

6 What's Next

Check the gotk3 documentation to explore all the GTK widgets available in Go: List Box, Notebook, File Chooser Dialog, ...

If the Go code looks cumbersome, there's another option for coding GTK apps in Go: Glade UI Builder for GTK

The output from Glade may be integrated into a Go app with gotk3 like this: gotk3-glade-example

The code in this article is part of the upcoming open source PineTime Companion App for Linux phones (like PinePhone). So that we can flash our PineTime Smart Watches wirelessly, sync the date and time, show notifications from our phone, chart our heart rate, ... Maybe even control our smart home gadgets!

If you're keen to help out, come chat with the PineTime FOSS Community (and me) in the PineTime Chatroom!

PineTime Chatroom on Matrix / Discord / Telegram / IRC

7 Further Reading

"Your First Bluetooth Low Energy App with Flutter"

"Convert Go to Flutter and Dart for PineTime Companion App"

"MCUBoot Bootloader for PineTime Smart Watch (nRF52)"

"Firmware Update over Bluetooth Low Energy on PineTime Smart Watch"

"Wireless Firmware Update In Action on PineTime Smart Watch (nRF52)"

Check out the other PineTime articles

RSS Feed