DEV Community

Cover image for Using Live Reload in Go
Gustavo Castillo
Gustavo Castillo

Posted on

Using Live Reload in Go

#go

When we're developing APIs in Go or any other language it's important to have tools that help us to automate certain tasks like Live Reload, I'd had been using realize to doing it, but it seems to have some issues when working with Go modules, I tried some different solutions and they didn't work for me 😭.

Spider Sad

Luckily I found a very handy package that can helps us to achieve this task adn it's called modd, let's give it a try and see how it works:

Create the basic project structure

$ mkdir live-reload && cd live-reload && mkdir cmd
$ go mod init
$ touch cmd/main.go
Enter fullscreen mode Exit fullscreen mode

Open main.go file and type the following

package main

import (
    "fmt"
    "log"
    "net/http"
)

func main() {

    mux := http.NewServeMux()

    mux.HandleFunc("/", homeHandler)
    mux.HandleFunc("/contact", contactHandler)

    log.Println(fmt.Sprintf("Server running on http://localhost%s 🐹", ":4000"))
    err := http.ListenAndServe(":4000", mux)
    if err != nil {
        log.Fatalf("could not run the server %v", err)
        return
    }
}

func homeHandler(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Hello from home handler"))
}

func contactHandler(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Hello from contact handler"))
}

Enter fullscreen mode Exit fullscreen mode

Installing modd package

$ brew install modd
Enter fullscreen mode Exit fullscreen mode

Create modd config file

# run _only_ changed tests
**/*.go {
    prep: go test @dirmods
}

# run and rebuild the app
# make sure you type your app name adter -o flag and daemon command
**/*.go !**/*_test.go {
    prep: go build -o live-reload ./cmd
    daemon +sigterm: ./live-reload
}

Enter fullscreen mode Exit fullscreen mode

Then execute modd command inside root project:

$ modd
Enter fullscreen mode Exit fullscreen mode

You should see something like this in your terminal:

modd running

Go to your main.go file and update homeHandler

//... other code
func homeHandler(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Hello from home handler changed and live reload 😎"))
}
//... rest of the code
Enter fullscreen mode Exit fullscreen mode

After that modd is going to re-compile your server and now you can visit http://localhost:4000 and see the updated version, your terminal should look like so:

Alt Text

Conclusion

Hope it helps, happy coding 👨🏽‍💻

Top comments (3)

Collapse
 
gypsydave5 profile image
David Wickes

If you like this idea, but want something a little more flexible without a config file, try entr:

eradman.com/entrproject/

You could probably achieve a similar result with entr, a sprinkle of shell scripting, and possibly a Makefile if you really wanted to.

Collapse
 
mohamed_saleh profile image
Mohamed Saleh

I'm using Watchman by Facebook to achieve this but I like the idea of a config file
Thanks!

Example of what I currently do:
watchman-make -p '**/*.go' --run ./whatever.sh

facebook.github.io/watchman/

Collapse
 
andreidascalu profile image
Andrei Dascalu

Loads of stuff can do this. I use CompileDaemon