Golang
To build your backend with Golang, you can use the go-partial library.
Most of the functionality is tailored to work seamlessly with the partial.js library.
However, it also integrates well with various frontend libraries, including:
- HTMX
- Turbo
- Stimulus
- Unpoly
Installation
To get started, install the go-partial library by running the following command:
go get github.com/partial-coffee/go-partial
Usage
After installing the library, import it into your project:
import "github.com/partial-coffee/go-partial"
Here is an example of how to use the library to handle a 404 error, wrapped by a general layout:
func (a *App) Handle404(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
l := a.BaseLayout()
// Get the partial template
fourOFour := partial.New(path.Join("templates", "404.gohtml")).ID("content")
// If targeting a specific element, use the target header to get the ID
if r.Header.Get(l.Connector().GetTargetHeader()) != "" {
fourOFour.ID(r.Header.Get(l.Connector().GetTargetHeader()))
}
// Set the partial to the main layout
l.Set(fourOFour)
// Write the response
err := l.WriteWithRequest(ctx, w, r)
if err != nil {
a.Logger.Error("failed to write response", err.Error())
}
}
That's it! You now have a working 404 page that can be used to show a 404 error in a specific target or when browsing to an invalid path.