Cookie Consent by Free Privacy Policy Generator 📌 Hot reloading in Go applications


✅ Hot reloading in Go applications


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

As someone who actively builds applications within the JavaScript / Node.js ecosystem leveraging frameworks like Next.js, Vue, Express.js, etc., hot reloading is usually built into the framework or needs to install a package to get it up and running.

While exploring the Go ecosystem, hot reloading seems like an afterthought in most of their popular frameworks. In fact, it became a source of concern for me and led me down the rabbit hole of finding possible solutions on how to include hot reloading in my Go applications.

This guide will explore what hot reloading is and why it is needed. It will also explore two libraries to help build hot reloading into your applications.

What is hot reloading?

Hot reloading is a technique used during application development, allowing you to observe updated changes to the source code in real-time without reloading the entire application. It preserves the application state, enabling you to continue working without losing the current state of the application. It allows you to view changes instantly, accelerating the development process and enhancing productivity.

What are the benefits of hot reloading?

The following are the benefits of enabling hot reloading in your applications:

  • Enhanced user experience
  • Fast development
  • Minimal context switching
  • Improved feedback loop
  1. Enhanced user experience: The ability to make changes to the codebase and see them in real-time improves the development process. With this capability, developers can rapidly prototype and design better applications.
  2. Fast development: With hot reloading, developers don't have to manually reload the application to see the latest changes. Development time is fast because changes are reflected immediately.
  3. Minimal context switching: A major source of distraction for developers is having to go back to the terminal to rebuild and run the application every time changes are made. However, with hot reloading, they can focus on the task without any interruptions or worries about their changes being reflected as updates occur in real-time.
  4. Improved feedback loop: Because changes are visible in real-time, developers receive immediate feedback and can easily fix bugs, identify where the application is broken, and consequently improve the development workflow.

How to implement hot reloading in a Go application

This section will explore two libraries that make hot reloading possible in a Go application:

  • Modd
  • Air

Before we explore how to use these libraries, let’s set up a sample Go project using the Gin-gonic framework.

Create a directory.

mkdir sample_go && cd sample_go

Next, initialize a Go module to manage project dependencies.

go mod init sample_go

Install the required dependency.

go get github.com/gin-gonic/gin

Create a sample application.

package main

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

func main() {
    r := gin.Default()
    r.GET("/", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "message": "Hello world!",
        })
    })

    r.Run()
}

Modd for hot reloading

Modd is a library that makes hot reloading possible in Go applications. To use it, install it on your machine using the command below:

go install github.com/cortesi/modd/cmd/modd@latest

In your project directory, create a modd.conf file and add the snippet below:

**/*.go {
    prep: go test @dirmods
}

The command above tells Modd to watch for changes to any .go file and run the changes on test directories.

To enable hot reloading, run the command below in your terminal:

modd

In case you get an error about go: errors parsing go.mod: /project/path/ invalid go version '1.XX.X': must match format 1.23. Remove the Go semantic version stated inside the go.mod file. For example change go 1.22.1 to go 1.22

Air for hot reloading

Air is another library that enables hot reloading in Go applications. To use it, install it on your machine using the command below:

go install github.com/cosmtrek/air@latest

Next, initialize Air by running the command below:

air init

The command above will create a .air.toml file with default configurations to get your application up and running.

Finally, to enable hot reloading, run the command below:

air

Conclusion

Both libraries enable hot reloading and offer extensive configuration and flexibility regarding which files and folders should be excluded or ignored, pattern matching, environment setup, and much more. To learn more, you can check out their official documentation.

Useful resources:

...

✅ How are Hot Reloading and Live Reloading in React Native different ?


📈 63.7 Punkte

✅ How are Hot Reloading and Live Reloading in React Native different?


📈 63.7 Punkte

✅ Hot reloading in Go applications


📈 45.25 Punkte

✅ Enabling Hot Reloading for Next.js in Docker


📈 37.2 Punkte

✅ Browser hot-reloading for Python ASGI web apps using arel


📈 37.2 Punkte

✅ Three Ways to Enable Hot Reloading in Express JS


📈 37.2 Punkte

✅ The Magic of Hot Reloading in React


📈 37.2 Punkte

✅ Mastering Blazor - Part 2: Hot Reloading


📈 37.2 Punkte

✅ CISSP IS HOT, HOT, HOT, SAYS CNBC


📈 32.08 Punkte

✅ Advanced JavaScript Technique: Reloading a Page With Location.reload(true)


📈 26.5 Punkte

✅ How to Reload the div Without Reloading Entire Page in jQuery


📈 26.5 Punkte

✅ Google Chrome 56 Makes Page Reloading 28 Percent Faster


📈 26.5 Punkte

✅ Safe CPU Temps: How Hot is Too Hot for a CPU?


📈 21.39 Punkte

✅ Mobilfunker - Diskonter Hot startet "Hot Business" für Geschäftskunden


📈 21.39 Punkte

✅ CordCutters Podcast Episode 7: Hot Hot Hardware


📈 21.39 Punkte

✅ Medium CVE-2017-17612: Hot scripts clone project Hot scripts clone


📈 21.39 Punkte

✅ Novell Filr bis 1.2 Hot Patch 5/2.0 Hot Patch 1 /etc/profile.d/vainit.sh erweiterte Rechte


📈 21.39 Punkte

✅ Novell Filr bis 1.2 Hot Patch 5/2.0 Hot Patch 1 /etc/profile.d/vainit.sh erweiterte Rechte


📈 21.39 Punkte

✅ CVE-2024-29796 | Hot Themes Hot Random Image Plugin up to 1.8.1 on WordPress cross site scripting


📈 21.39 Punkte

✅ Hot keys for Chrome or a better browser for searching w/hot keys


📈 21.39 Punkte

✅ How Hot Is Too Hot? #Shorts


📈 21.39 Punkte

✅ Deep Instinct Prevention for Applications defends mission critical business applications


📈 16.11 Punkte

✅ How do move my applications onto the desktop. I tried looking in usr/share/applications and its not there.


📈 16.11 Punkte

✅ AI Will Disrupt How Developers Build Applications and the Nature of the Applications they Build


📈 16.11 Punkte

✅ AI Will Disrupt How Developers Build Applications and the Nature of the Applications they Build


📈 16.11 Punkte

✅ Single Page Applications(SPA) Vs Multi Page Applications(MPA)


📈 16.11 Punkte

✅ Scalable web applications | HTTP | REST API | N-tier Applications | Beginner


📈 16.11 Punkte

✅ Simulating Honey And Hot Showers For Bunnies! ??


📈 10.69 Punkte

✅ Vorbestellen: Handelsversion von Need for Speed: Hot Pursuit Remastered für die Nintendo Switch


📈 10.69 Punkte

✅ NetMotion erhält den IT World Gold Award 2018 in der Kategorie Hot Technology


📈 10.69 Punkte

✅ [webapps] Hot Scripts Clone 3.1 - 'subctid' / 'mctid' SQL Injection


📈 10.69 Punkte











matomo

Datei nicht gefunden!