Developing Go APIs with Live Reloading Using GoAir and Docker ????

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:

  • Installs Go Air for live reloading.
  • Copies the code and installs dependencies from go.mod.
  • Exposes port 8080, where the Go app will run.
  • Starts Go Air with the .air.toml configuration file.

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:

  • Which directories to exclude from live reloading.
  • To poll for changes instead of using file system notifications.
  • To rebuild the app every time changes are detected in relevant files (like .go or .html).

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:

  • The app service builds the image using the provided Dockerfile.
  • We map the current directory (our codebase) to the /app directory inside the container.
  • Port 8080 is exposed for the API.

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 ??

  1. Live Reloading: No more manual restarts. Changes are reflected instantly.
  2. Consistent Environment: Docker ensures your app runs the same way everywhere.
  3. Portable: Share your Dockerfile and docker-compose.yml with your team for a seamless setup.

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!

Omar Haris

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! ??

Avdhoot Jadhav

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.

Nithish Suresh

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!! ??

Sumit Yadav

Sr SWE @EagleView | Writing Backend Systems for Ariel Imagery | Past - Amazon, Amdocs

2 周

supported after go 1.23 only

Jardel Moraes

Data Engineer | Python | SQL | PySpark | Databricks | Azure Certified: 5x

2 周

Thank you for such valuable info! ??

回复

要查看或添加评论,请登录

Auber Mardegan的更多文章