24
Go🐹 live-Reload✳ using Air🌫
First post jitters... Yes indeed, I'm relatively new to DEV community(started yesterday) and (sharing in general) writing blog posts.
Bit about me to break the ice🧊 if you will, I took I.T as a subject for 3 years in high school where I learnt Java and SQL primarily. Then decided to venture into the world of design after school and became a multimedia designer🧙🏾♂️(yaaay!) end of intro... not really but long story short I am now doing a B.Sc in Business and Comp Sci and transitioning into a full-stack dev👨🏾💻
I've recently come across Golang as a fresh language that's growing quite quickly and can be compared to Java, while I have worked with Java before and I'm currently studying Java - so Go is not exactly foreign by comparison, personally I would much prefer using and learning Go for numerous reasons that one might find on the internet.
Back to this here post... if you've used Go you would know that is a compiled language meaning you can see if you've main any errors prior to build time, but when developing REST API's using Go Fiber(an Express inspired Web Framework) you might have noticed that Go does not have a built in live-reload so you'd have to manually "go run main.go" each time you update or add a new file. Fortunately we have the internet and my all-round sense of optimism(basically always believing someone's invented a work-around). Low and behold I came across Air which is a "command line utility for Go applications in development." - according to their GitHub repo
Super simple to use: // I'm using Windows // This is a local development example
Step 1 - Have Go installed
Step 2 - Assuming you installed correctly and added GOPATH/bin
to your PATH
variable, cd
into your go-workspace(GoPath).
Step 3 - Install Go Fiber in your terminal: go get -u github.com/gofiber/fiber/v2
to install Fiber.
Step 4 - Using your IDE of choice(I ofc use VSCode issa Windows ting) in the terminal mkdir go-air
cd go-air
go mod init go-air
this will create a go.mod file to keep track of your package modules in your own module(go.mod) simple.
Step 5 - I'm going to be using Go Fiber for this example, if you know Go then you know we need a main.go file next so create that if you haven't. Copy the below code into it:
_// if you're using React to dev your front-end change 3000 to 8000 or don't your choice(just do it✔)
package main
import "github.com/gofiber/fiber/v2"
func main() {
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, World 👋!")
})
app.Listen(":3000")
}
Step 6 - Next we add the Air🌫 in your terminal go get -u github.com/cosmtrek/air
this will add Air, you'll see it in the go.mod when it's done(it will also be in your GOPATH/bin)
Step 7 - Create a file called: .air.conf
inside paste the below code:
# .air.conf
# Config file for [Air](https://github.com/cosmtrek/air) in TOML format
# Working directory
# . or absolute path, please note that the directories following must be under root.
root = "."
tmp_dir = "tmp"
[build]
# Just plain old shell command. You could use `make` as well.
cmd = "go build -o ./tmp/main ."
# Binary file yields from `cmd`.
bin = "tmp/main"
# Customize binary.
full_bin = "APP_ENV=dev APP_USER=air ./tmp/main"
# Watch these filename extensions.
include_ext = ["go", "tpl", "tmpl", "html"]
# Ignore these filename extensions or directories.
exclude_dir = ["assets", "tmp", "vendor", "frontend/node_modules"]
# Watch these directories if you specified.
include_dir = []
# Exclude files.
exclude_file = []
# It's not necessary to trigger build each time file changes if it's too frequent.
delay = 1000 # ms
# Stop to run old binary when build errors occur.
stop_on_error = true
# This log file places in your tmp_dir.
log = "air_errors.log"
[log]
# Show log time
time = false
[color]
# Customize each part's color. If no color found, use the raw app log.
main = "magenta"
watcher = "cyan"
build = "yellow"
runner = "green"
[misc]
# Delete tmp directory on exit
clean_on_exit = true
// ofc save this file and don't forget to save the main.go file with the Go Fiber code
I won't go through everything as it's pretty much written in English, if you give it a scan you'll want to keep track of the words Watch and Exclude as this is basically what Air does is watch for updates(or exclude them) then initiates a shell command to reload/rerun/rebuild (which ever works for you) the main.go file.
Finally - Last Step - Actually the most important, I ran into some issues running Air from my terminal(PowerShell) what you need to do basically is run air.exe(it should be in GOPATH/bin) from your root directory(go-air) which should be in your GOPATH(go-workspace). I found GitBash useful for this so if you have bash installed open up a bash terminal(still in your IDE from go-air) and type ~/GOPATH/bin/air
and that's it. Air should run in your bash terminal now. Update the main.go file and see the magic✨
Anyway that was my first post, think it went well - holla at ya boi👨🏾💻 sometimes I tweet(most of the time I don't) follow me @get_komfy SALUTAS Keep it komfy
24