Lesson Description

The "Install AIR" Lesson is part of the full, Build a Fullstack App with Vanilla JS and Go course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano walks through installing a tool called "Air" that provides live reload for Go apps. He demonstrates how to install the tool using the command line and explains that it automatically rebuilds and runs the server whenever changes are made to the Go files.

Preview
Close

Transcript from the "Install AIR" Lesson

[00:00:00]
>> Maximiliano Firtman: If I save this, okay, I don't have any errors. I have a couple of warnings that we can solve later, but I don't have any errors. So if I go to the browser and refresh, I'm still seeing 404, why, does anyone know why? So it's /API, /movies, /Top.

[00:00:22]
And if I go to my main.go, it's exactly that URL, that path that I'm handling, but it's not working, does anyone know why?
>> Student: Restarted it?
>> Maximiliano Firtman: I need to restart the server. So now is the time that we realize that keeping the server open and running manually is not really helpful, because we will start complaining every time that we try to refresh and it's not working.

[00:00:52]
And that's because we didn't actually restart the server. So I can do it manually, but maybe it's time now to install one of the tools that will help us with this, okay? So if you go to README, you have the link to go directly to that part.
>> Maximiliano Firtman: It's a five, I think, yep, a five.

[00:01:18]
We have the link to github to air and it says, Live reload for Go apps, that's exactly what we need. So this is a tool that you need to install. It's a command line tool that you need to install in your operating system. So it's not actually a dependency of my project.

[00:01:42]
It's a tool that we need to add to the command line interface. In modern Go, there are many ways that you can install this, but in modern Go, you can directly from go 1.23, so you need that version at least. You can use go Install, directly from the common line.

[00:02:02]
If you have a previous version of Go, you need to find another way to install these tools that depends on your operating system, so you can follow the instructions there. But if you are, I'm in 1.24, so of course it's greater than 1.23. So I can go here, open my terminal Ctrl C, [INAUDIBLE] stop the server and I can install this tool.

[00:02:24]
If it works, it will download the tool, and it will put the tool in a binaries folder inside go that should be in your path. If everything works, you can now type air and something will happen. You will see the AIR tool that is saying by default, it's watching.

[00:02:49]
Also, it's watching assets, hundreds, so it's watching all my folders. So what's the deal? If I make any change in any of my Go files, for example here, and now I changed this and another forward slash. If I save, you can see it recognized that main.go has changed and it's building and running again.

[00:03:16]
So automatically now I can go to the browser, refresh and I will get the final version, okay? Does it make any sense? So now we won't run our project using GoRun. We will run our project using air. Do you have any question on air, on using air as your tool to run your server?

[00:03:40]
We're still not done with air, why? Remember we have an HTML here, so if I go and open localhost 8080,
>> Maximiliano Firtman: It says, Hello from Go. So now, If I'm going to that HTML, remember it's in public, index.html, and I change this, Hello from Go using Air. If I save the file, the problem is that it recognizes that index.html has changed, and it's also recompiling Go.

[00:04:19]
And it doesn't make any sense. It is not the big problem, but now every time we make a change in HTML, CSS, or JavaScript, so in the frontend, it's also recompiling the backend which is not what I need. Because I'm not changing the backend, okay, does that make sense?

[00:04:39]
So we need to do something else, and we need to set up air to just look into go files. Those are the only files that I care about, okay? Does it make sense? So, to solve that problem, in the README file, you already have the solution. We need to create a file .air, .toml, so I need to create a new file in the root of my project.

[00:05:07]
So in the root I will create a file, .air, .toml, and you already have here the configuration file that we can copy and paste. And if you look at it, what it says is that it will include the extension go. So it's only watching go files, and also I can exclude directories, and if you want, you can say public here.

[00:05:41]
So you can add all the files that you actually don't care about. So you know that I don't have their go files, so even if I have a go file there, I don't wanna look at it. So now, if you restart air. So if I restart air, now it's watching only for go files.

[00:06:05]
Okay, because I do have that file in the root.
>> Maximiliano Firtman: Again, in this case, we are having better performance, because if not, you change the HTML and we will start working with the frontend in probably a couple of minutes, or maybe one hour. And when we do that, every time we make a change in JavaScript, I don't want the server to restart, and if I do that, I will need to wait like two three seconds before reloading the client, which makes no sense.

[00:06:40]
Okay, make sense? So now we have air installed, we can try and see if we have our API. So if I go to local host 8080 api/movie/Top, I can see my dummy data, dummy movies.

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