html is a Go package that provides a set of types, interfaces and functions for creating UI components directly in your Go source code.
The package provides an Element type that represents an HTML DOM node:
type Element struct {
Tag string
Attrs Attrs
Children []Element
}as well as a Component interface:
type Component interface {
Render() Element
}To turn any existing Go type into an html.Component simply add a Render() method.
For example:
func (mt *MyType) Render() html.Element {
return html.Element{
Tag: "button",
Attrs: html.Attrs{
"class": "bg-red-700 drop-shadow rounded-lg p-4",
},
Children: []html.Element{
html.Text(mt.Name),
},
}
}