Developing Go APIs with Live Reloading Using GoAir and Docker ????
If you're a Go developer, you know how frustrating it can be to stop, rebuild, and restart your application every time you make a change to your code. What if I told you there's a way to automate this process and achieve live reloading in your Go applications? Enter Go Air, a live-reloading tool for Go, combined with Docker for a seamless development experience.
In this article, I'll walk you through setting up Go Air with Docker, and we'll build a simple API that automatically reloads whenever you make changes to your code. Let’s dive in!
What is Go Air?
Go Air is a command-line tool that monitors your Go project for changes and automatically rebuilds and restarts your application. It’s perfect for development environments where you want to see your changes reflected instantly without manually restarting your server.
Why Use Docker with Go Air?
Docker allows you to containerize your application, ensuring a consistent environment across different machines. By combining Docker with Go Air, you can create a development environment that’s both portable and efficient.
Setting Up Go Air with Docker
Let’s walk through setting up a Go project with live reloading using Go Air inside Docker. The example will involve creating a simple API that reloads when modified.
Step 1: Install Go Air Locally
To start using Go Air, you'll need to install it. You can install it using go install:
Step 2: Create the Dockerfile
Here’s the Dockerfile we’ll use, which sets up a Go environment with Go Air installed for live reloading.
This Dockerfile does the following:
Step 3: Create the .air.toml Configuration
The .air.toml file configures how Go Air behaves. We generated this file using the command air init and added a few tweaks to optimize our setup. Specifically, we enabled the poll flag and set the polling interval to 500ms to ensure the app quickly responds to file changes.
Here’s the content of the .air.toml file:
This file tells Go Air:
Step 4: Add docker-compose.yml for Easier Setup
Now, let’s make the process even smoother by using Docker Compose to build and run the container. Here’s our docker-compose.yml:
The key components here:
Step 5: Writing the Go API (main.go)
Let’s create a simple API with a GET request to verify our setup.
Step 6: Building and Running the Docker Container
Build and start the Docker container using Docker Compose:
Docker will now set up everything, including live reloading, and map your local directory inside the container. The Go app will run on https://localhost:8080.
Open your browser and navigate to https://localhost:8080. You should see "Hello, World!" displayed
Step 7: Testing Live Reloading
Let’s test the live reloading feature. Update the main.go file to change the response message:
Save the file, and you’ll see Go Air automatically rebuild and restart your application in the terminal. Refresh your browser, and the new message should appear with no manual restart required!
Conclusion and Key Takeaways ??
Using Go Air with Docker is a game-changer for Go developers. It streamlines your workflow, reduces friction, and lets you focus on writing code instead of managing your development environment. Give it a try in your next project, and let me know how it works for you!
Software Architect | Senior Golang Developer | Technical Lead | Digital Transformation | AI | Video Streaming Transcoding
3 天前I find live reload unnecessary. I prefer finishing my code first, then testing it to make sure everything works perfectly like a hero! ??
Associate SDE at Swiggy
4 天前Interesting article. Just curious if this works when we are running go app locally and we can have hot reload.
Software Developer | Full Stack Developer | Backend Engineering | Golang | Python | SaaS | IaaS | Databases | AWS Solutions Architect | GCP | DevOps | Artificial Intelligence | Data Engineering
2 周Auber Mardegan, this is what I wanted exactly!! ??
Sr SWE @EagleView | Writing Backend Systems for Ariel Imagery | Past - Amazon, Amdocs
2 周supported after go 1.23 only
Data Engineer | Python | SQL | PySpark | Databricks | Azure Certified: 5x
2 周Thank you for such valuable info! ??