This is a premium alert message you can set from Layout! Get Now!

Using Air with Go to implement live reload

0

Live reload is a useful feature that allows developers to see their changes in real-time as they work on their code. This can save a lot of time because it eliminates the need to manually stop and restart the server whenever you make a change.

There are several live reload frameworks for Go that you can use to automatically reload your application whenever you make changes to your source code. This blog post will look at how to use the Air library to implement live reload in Go.

Jump ahead:

Before we get into how to implement live reloading in Go, let’s talk about why you might want to use live reload in the first place.

Why use live reload in Go?

One of the primary advantages of live reloading is that it significantly accelerates your development workflow.

Instead of stopping and restarting your application every time you make a change, you can simply save your changes and have your application reload itself. This can save you time and frustration, especially if you’re working on a large application or one that takes a long time to fire up.

Live reloading can also be useful for debugging. By automatically reloading the application when you make a change, you can identify and resolve problems more quickly.

This is especially useful when working on applications that are difficult to debug, such as those with complex dependencies or long startup times.

Live reload vs. hot reload in Go

In Go, “live reload” refers to the ability to automatically reload and update an application without needing to stop and restart it manually.

“Hot reload” is a similar concept, but specifically refers to the ability to update the application’s code while it is still running — without interrupting its current state or any ongoing processes.

Both live reload and hot reload can be helpful in development, as they allow for faster iteration and testing of code changes without needing to stop and restart the application manually.

However, depending on the complexity of the application, hot reloading may not be possible or may be more challenging to implement.

Go and Gin projects do not have native support for live reload functionality, so let’s see how to set up the Air library to implement live reload in Go and Gin projects.

Introduction to the Air library for Go applications

Air is a live-reloading command line utility for Go applications in development. It is a command line tool that allows Go applications to reload in real time.

Besides Air, other popular live reload frameworks for Go include Fresh and Realize. They all work in similar ways — by automatically rebuilding and restarting your application whenever they detect changes to your source code.

Fresh is another command line tool intended for use with any Go web server that is simple to integrate into your build process.

Realize is a Go-based build system that provides live reloading functionality for Go applications.

Since these options all work in very similar ways, deciding which framework to use in your Go project is mostly a matter of preference. Try each one out to see what feels the easiest or most intuitive to you.

Guide to using Air in Go and Gin projects

You can easily get started with Air in Go and Gin projects by following these five steps. First, install the Air CLI tool by running the following command:

go get -u github.com/cosmtrek/air

Next, install the Gin web framework by running the following command:

go get -u github.com/gin-gonic/gin

Create a main.go file and declare a simple Hello, World! endpoint in Gin:

package main

import (
    "github.com/gin-gonic/gin"
)

func main() {
    r := gin.Default()
    r.GET("/", func(c *gin.Context) {
        c.String(200, "Hello, World!")
    })
    r.Run()
}

Create an .air.toml file in the root of your project. This file will be used to configure Air. You can use the following configuration options:

root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"

[build]
  args_bin = []
  bin = "./tmp/main"
  cmd = "go build -o ./tmp/main ."
  delay = 1000
  exclude_dir = ["assets", "tmp", "vendor", "testdata"]
  exclude_file = []
  exclude_regex = ["_test.go"]
  exclude_unchanged = false
  follow_symlink = false
  full_bin = ""
  include_dir = []
  include_ext = ["go", "tpl", "tmpl", "html"]
  kill_delay = "0s"
  log = "build-errors.log"
  send_interrupt = false
  stop_on_error = true

[color]
  app = ""
  build = "yellow"
  main = "magenta"
  runner = "green"
  watcher = "cyan"

[log]
  time = false

[misc]
  clean_on_exit = false

[screen]
  clear_on_rebuild = false

Finally, run the Gin application from the terminal using Air:

air

This will start the server and watch for changes to your templates and static assets. When a change is detected, the server will automatically reload to reflect the changes.

Guide to using Air with Docker containers

One of the main benefits of using live reload with a Docker container in your Go application is that it allows you to iterate quickly on your code without stopping and starting the container each time.

This can be especially useful when developing and testing applications inside a container, as it can help you identify and fix issues more quickly and easily.

To use the live reload library with Docker, you’ll need to mount your local source code directory as a volume in the Docker container. This will allow the Air library to watch for changes to your code and automatically reload the server when necessary.

First, create a Dockerfile with instructions to install and run Air:

FROM golang:latest

# Set the current working directory inside the container
WORKDIR /app

# Copy go.mod and go.sum files to the workspace
COPY go.mod go.sum ./

# Download all dependencies
RUN go mod download

# Copy the source from the current directory to the workspace
COPY . .

# Build the Go app
RUN go build -o main .

# Expose port 8080 to the outside world
EXPOSE 8080

# Command to run the executable
CMD ["air"]

Next, create an .air.toml file in the root of your project containing the following:

root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"

[build]
  args_bin = []
  bin = "./tmp/main"
  cmd = "go build -o ./tmp/main ."
  delay = 1000
  exclude_dir = ["assets", "tmp", "vendor", "testdata"]
  exclude_file = []
  exclude_regex = ["_test.go"]
  exclude_unchanged = false
  follow_symlink = false
  full_bin = ""
  include_dir = []
  include_ext = ["go", "tpl", "tmpl", "html"]
  kill_delay = "0s"
  log = "build-errors.log"
  send_interrupt = false
  stop_on_error = true

[color]
  app = ""
  build = "yellow"
  main = "magenta"
  runner = "green"
  watcher = "cyan"

[log]
  time = false

[misc]
  clean_on_exit = false

[screen]
  clear_on_rebuild = false

To build the Docker image, run the following command:

docker build -t airy-app .

This will create a Docker image called gin-app that you can use to run your Gin server. To run the server in a Docker container, use the following command:

docker run -p 8080:8080 -v $PWD:/app airy-app

This will start the Gin server in a Docker container and expose it on port 8080. You can then access the server at http://localhost:8080.

Summary

Air is a live reloading tool for Go that allows developers to automatically recompile and run their code every time they make changes.

With Air, developers can quickly implement live reloading in their Go projects, saving them time and effort during the development process. This can be especially useful for developing web applications, as it allows developers to see their changes in real-time without having to stop and start the server manually.

Overall, Air is a valuable tool for Go developers looking to streamline their workflow and improve productivity.

The post Using Air with Go to implement live reload appeared first on LogRocket Blog.



from LogRocket Blog https://ift.tt/d2zlf7D
Gain $200 in a week
via Read more

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment

Search This Blog

To Top