reload

package module
v1.4.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2022 License: MIT Imports: 8 Imported by: 11

README

GoDoc Build Status codecov

Lightweight automatic reloading of Go processes.

After initialisation with reload.Do() any changes to the binary (and only the binary) will restart the process. For example:

func main() {
    go func() {
        err := reload.Do(log.Printf)
        if err != nil {
            panic(err) // Only returns initialisation errors.
        }
    }()

    fmt.Println(os.Args)
    fmt.Println(os.Environ())
    ch := make(chan bool)
    <-ch
}

Now use go install or go build to restart the process.

Additional directories can be watched using reload.Dir(); this is useful for reloading templates:

func main() {
    go func() {
        err := reload.Do(log.Printf, reload.Dir("tpl", reloadTpl))
        if err != nil {
            panic(err)
        }
    }()
}

You can also use reload.Exec() to manually restart your process without calling reload.Do().


This is an alternative to the "restart binary after any *.go file changed"-strategy that some other projects – such as gin or go-watcher – take. The advantage of reload's approach is that you have a more control over when the process restarts, and it only watches a single directory for changes which has some performance benefits, especially when used over NFS or Docker with a large number of files.

It also means you won't start a whole bunch of builds if you update 20 files in a quick succession. On a desktop this probably isn't a huge deal, but on a laptop it'll save some battery power.

Because it's in-process you can also do things like reloading just templates instead of recompiling/restarting everything.

Caveat: the old process will continue running happily if go install has a compile error, so if you missed any compile errors due to switching the window too soon you may get confused.

Documentation

Overview

Package reload offers lightweight automatic reloading of running processes.

After initialisation with reload.Do() any changes to the binary will restart the process.

Example:

go func() {
    err := reload.Do(log.Printf)
    if err != nil {
        panic(err)
    }
}()

A list of additional directories to watch can be added:

go func() {
    err := reload.Do(log.Printf, reload.Dir("tpl", reloadTpl)
    if err != nil {
        panic(err)
    }
}()

Note that this package won't prevent race conditions (e.g. when assigning to a global templates variable). You'll need to use sync.RWMutex yourself.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dir

func Dir(path string, cb func()) dir

Dir is an additional directory to watch for changes. Directories are watched non-recursively.

The second argument is the callback that to run when the directory changes. Use reload.Exec() to restart the process.

func Do

func Do(log func(string, ...interface{}), additional ...dir) error

Do reload the current process when its binary changes.

The log function is used to display an informational startup message and errors. It works well with e.g. the standard log package or Logrus.

The error return will only return initialisation errors. Once initialized it will use the log function to print errors, rather than return.

func Exec

func Exec()

Exec replaces the current process with a new copy of itself.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL