This lesson is locked. Login or Subscribe for more access!

Building Desktop apps using Go

Duration: 0 mins

Did you know you can build Mac, Linux & Windows apps using Go? Learn how!

Instructor

Matt Boyle

Share with a friend!

Transcript

So what you're seeing on my screen now is a native Mac application written in Go. As you can see, the Rust Crab has taken over my Goland IDE, and I've got to kill them with Go functions. As you can also see, I'm not very good at this game, but that's not the point. The point is that you really can make native Mac applications using Golang. So let's talk a little bit about how to do that. And it’s not just Mac, by the way; you can make Windows applications and Linux applications too.

To do this, we're using something called Wails. Wails is a cross-platform toolchain to help you build native applications using Go and web technologies that you already know how to use. In this video, I'm going to give you a speedrun of the concepts so that you can go ahead and make your own.

Firstly, you need to install Wails, which you can do from their site—it gives you instructions on how to do it. You can simply run the go install command to do so, so it’s nice and easy. The next thing you need to do is go to your terminal. I’m just going to cd into my dev folder and make it a bit bigger. All you need to do is run wails init -n native-app to give your application a name—so I’m calling mine "native app" for simplicity’s sake. Let’s generate it and then open it in Goland.

Once you’ve got it open, you’ll see a package structure that looks something like this. It’s fairly well divided, so it’s fairly clear, but we have two folders that we effectively need to care about: one is frontend, and the other is app.go. Inside frontend, the main thing we’re interested in is index.html. This is effectively where you can put all your frontend logic—the bit you want the user to see—and we’ll see a little bit more about that in a moment. The other thing we need to do is look at app.go. As you can see, there’s nothing too crazy here; this is fairly reasonable Go that you can reason about. We have this new app concept, a startup function, and then we have this greet function, which lives on the app struct.

This is really important because what happens is this greet function actually becomes available to you in JavaScript, which is kind of crazy. If you follow this down the rabbit hole, you’ll see that greet is generated by Wails and available in the window object. So you can actually call greet within your JavaScript function. The way this works is as follows: you have this Go runtime and these JS bindings for Go methods. When you do wails dev or wails build, it generates those bindings for you, so you’re able to call Go functions from within your JavaScript.

Let’s see this in action with the application we just generated. When you generate an application with Wails, it gives you some help by providing a really nice sample starter. So I’m just going to run wails dev, and it’s going to generate this app here. I’m going to put "Matt" in here—hello Matt, it’s your time. So where did that logic come from? You’ll see that it actually came from this greet function in Go. If we were to change this to say "it’s time for Byte Size Go," you’ll see that it automatically recompiles, and it comes up again, and it’s worked. So you can see how easy it is here to make a Go application that becomes a native app.

Then all we have to do to compile it is—I’m just going to close that—and run wails build. Because I didn’t specify a target (which you can do—you can specify Windows or Linux as a target), it’s used my Go OS target, which for me is a Mac. So if I head over to this location now, cd into the place where it generated my app, and open it, here’s my app. If I open this now, it really is like a native Mac application. You could ship this to the Mac OS App Store, and other people would be able to download it.

So what sort of stuff could you build with this? You could basically get anything on the app store you could. Because it has the Go backend component, you could easily follow your standard service pattern to add a database to this. You could make API calls. So you could build effectively anything you could build on the web using Go and Go templates—you’d be able to convert it into Wails. One project I’ve been working on in the background is to convert Byte Size Go, the website, into a native Mac application, just to follow my curiosity and see what that experience is like.

So have a go with Wails and see what you think. Building native Go applications with Go isn’t the first thing you think of when you think of the Go language. You’ve heard of Electron and some of these other technologies to enable this, but I think it’s really cool that we’re able to do this within the Go ecosystem too.