Book Image

Hands-On GUI Application Development in Go

By : Andrew Williams
Book Image

Hands-On GUI Application Development in Go

By: Andrew Williams

Overview of this book

Go is often compared to C++ when it comes to low-level programming and implementations that require faster processing, such as Graphical User Interfaces (GUIs). In fact, many claim that Go is superior to C++ in terms of its concurrency and ease of use. Most graphical application toolkits, though, are still written using C or C++, and so they don't enjoy the benefits of using a modern programming language such as Go. This guide to programming GUIs with Go 1.11 explores the various toolkits available, including UI, Walk, Shiny, and Fyne. The book compares the vision behind each project to help you pick the right approach for your project. Each framework is described in detail, outlining how you can build performant applications that users will love. To aid you further in creating applications using these emerging technologies, you'll be able to easily refer to code samples and screenshots featured in the book. In addition to toolkit-specific discussions, you'll cover more complex topics, such as how to structure growing graphical applications, and how cross-platform applications can integrate with each desktop operating system to create a seamless user experience. By delving into techniques and best practices for organizing and scaling Go-based graphical applications, you'll also glimpse Go's impressive concurrency system. In the concluding chapters, you'll discover how to distribute to the main desktop marketplaces and distribution channels. By the end of this book, you'll be a confident GUI developer who can use the Go language to boost the performance of your applications.
Table of Contents (25 chapters)
Title Page
Copyright and Credits
About Packt
Contributors
Preface
Comparison of GUI Toolkits
Index

Cross compiling for Linux with CGo


To cross compile for Linux, we'll need a GCC or compatible compiler that can build Linux binary files. On macOS, the easiest platform to use is musl-cross (musl has many other advantages that you can read more about at www.etalabs.net/compare_libcs.html). On Windows, the linux-gcc package will be suitable. Let's work through the steps for each of these.

 

 

To Linux from macOS

To install the dependencies for cross compiling for Linux, we'll use the Homebrew package manager again—see the previous sections or https://brew.sh/ for installation instructions. Using Homebrew, we'll install the appropriate packages by opening a Terminal and executing the following commands (the HOMEBREW_BUILD_FROM_SOURCE variable works around an issue with musl-cross depending on potentially old versions of libraries):

  • export HOMEBREW_BUILD_FROM_SOURCE=1
  • brew install FiloSottile/musl-cross/musl-cross

Once the installation is complete (this may take some time as it's building a complete compiler toolchain from source), you should be able to build for Linux. To do so, you'll need to set the environment variables, CC=x86_64-linux-musl-gcc and CXX=x86_64-linux-musl-g++.

To Linux from Windows

Using MSYS2 as earlier, we can install the gcc package to provide cross compilation for Linux:

pacman -S gcc

After the installation has completed, we can tell our Go compiler to use gcc by setting the environment variable CC=gcc. Compilation should now succeed following the instructions in your current example, such as the following:

GOOS=linux CGO_ENABLED=1 CC=gcc go build

It's possible, at this point, that you may see additional errors due to missing headers. To fix this, you'll need to search for, and install, the required libraries. If, for example, your error stated that SDL couldn't be found then you would use pacman -Ss sdl to search for the right package to install. If you can't find an appropriate package, you may need to install Cygwin www.cygwin.com/ (as it has a larger package library) or Windows subsystem for Linux docs.microsoft.com/en-us/windows/wsl/ (as that brings a full Linux distribution to your Windows desktop).