Check out a free preview of the full Basics of Go course

The "Compiling & Packaging" Lesson is part of the full, Basics of Go course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano covers the process of compiling a Go project, including the flexibility to compile for various platforms and operating systems. Additionally, in order to embed an application's assets, a package needs to be created, as Go produces binary files through compilation.

Preview
Close

Transcript from the "Compiling & Packaging" Lesson

[00:00:00]
>> Final steps here. So let's say that we are done with our Go project. Our Go project is ready. What are the final steps? Well, compiling the project, we haven't done that, okay? So so far, we haven't compiled the project. So to compile the project, it's go build ., and it will build the project.

[00:00:17]
You can also specify an output folder, because by default, it will just generate the executable in your current folder. And also you can set environmental variables to change the OS and the architecture. Because by default, it's going to use your OS and your architecture for the compilation. But if you're on a Mac, you can also compile for Windows.

[00:00:40]
So if I'm going to do that, Let's do it, if I can, there we are. So go build ., I'm going to build the current project. Great feedback, nothing. But I'm not sure if you realize there is a new file in town, museum, no extension, no nothing. What's that?

[00:01:09]
An executable, if you're on Windows, you will see museum.exe. On Linux or Mac, it's just the file. So if you wanna run it, you use ./ and the name of the executable, okay? And now it's running. Now my app is running, okay? So that's how you build your app.

[00:01:36]
It's very simple. What about the installation? It says here, go install. What's install? Go has a folder in your computer that is in the path. So if you're doing a CLI, this is only for CLIs. So if you want now your app to be available in the terminal everywhere, you install it.

[00:01:58]
And then you can run museum from anywhere, okay? What about packaging? Because that's the file there. I mean, if you're on a Mac, you know that to install an app, you typically download something, and then you need to put that into application folder. So do I need to just take that file as it is and put it there?

[00:02:17]
No, that's packaging, and Go ends with a binary. That's all, the binary. It's your work to add a package for your operating system in case you need to. Because maybe you're going to deploy this in the cloud, and each provider will tell you the instructions on where to put the binary and how to do it.

[00:02:42]
So you don't need to create the package. So, for example, the package is useful for embedding assets, because here, we have only the executable. But if we change the executable, our public folder will not go with the executable. So when we are building, we are just compiling the Go code.

[00:03:02]
Our public folder is not going to be in that executable file. So we are not embedding any assets. Does it make sense? Images, logo, PDF, whatever you need or you are pointing in your code is completely separate from the binary. So for that, you need to create the package and add your embeddings.

[00:03:23]
You can create the installer for Windows. There are a lot of tools out there that will let you do that. On the Mac, you need to learn how to create a DMG package. They can be actually just a package, or you can also create an installer. And something similar or different kind of packages for Linux based on the distribution, but the responsibility ends there.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now