gomponents
Original:🇺🇸 English
Translated
Guide for working with gomponents, a pure Go HTML component library. Use this skill when reading or writing gomponents code, or when building HTML views in Go applications.
163installs
Sourcemaragudk/skills
Added on
NPX Install
npx skill4agent add maragudk/skills gomponentsTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →gomponents
Overview
gomponents is a pure Go HTML component library that treats HTML elements as composable Go values. Everything is built on the interface, making HTML construction type-safe and composable.
NodeWhen to Use This Skill
Use this skill when:
- Reading or writing gomponents code
- Building server-side HTML views in Go applications
- Creating reusable HTML components in Go
Core Interface
Everything in gomponents implements the interface:
Nodego
type Node interface {
Render(w io.Writer) error
}Essential Functions
Element and Attribute Creation
- - Create custom HTML elements
El(name string, children ...Node) - - Create custom attributes
Attr(name string, value ...string)
Most standard HTML5 elements and attributes are available as functions in the package:
html- Elements: ,
Div(),Span(),P(), etc.A() - Attributes: ,
Class(),ID(),Href(), etc.Src()
Note: Nodes are ignored during rendering, so it's safe to pass nil nodes to elements.
nilText Content
- - HTML-escaped text content
Text(string) - - Formatted, escaped text
Textf(format string, args...) - - Unescaped HTML
Raw(string) - - Formatted, unescaped content
Rawf(format string, args...)
Composition
- - Combine multiple nodes
Group([]Node) - - Transform slices into node sequences
Map[T]([]T, func(T) Node) - - Conditional rendering
If(condition bool, node Node) - - Lazy conditional rendering (deferred evaluation)
Iff(condition bool, func() Node)
Import Convention
Contrary to common Go idioms, dot imports are recommended for gomponents to achieve DSL-like syntax:
go
import (
. "maragu.dev/gomponents"
. "maragu.dev/gomponents/html"
. "maragu.dev/gomponents/components"
)This allows writing clean, HTML-like code:
go
Div(Class("container"),
H1(Text("Hello World")),
P(Text("Welcome to gomponents")),
)Package Organization
- - Core interface and helper functions
maragu.dev/gomponents - - All HTML5 elements and attributes
maragu.dev/gomponents/html - - HTTP helpers for rendering components as responses
maragu.dev/gomponents/http - - Higher-level utilities (HTML5 document structure, dynamic classes)
maragu.dev/gomponents/components
Common Patterns
Basic Component
go
func UserCard(name, email string) Node {
return Div(Class("user-card"),
H2(Text(name)),
P(Text(email)),
)
}Conditional Rendering
go
func Alert(message string, isError bool) Node {
return Div(
If(isError, Class("error")),
If(!isError, Class("info")),
P(Text(message)),
)
}Use when the node is always safe to evaluate. Use when the node might be nil and shouldn't be evaluated unless the condition is true.
IfIffgo
func UserProfile(user *User) Node {
return Div(
H1(Text(user.Name)),
// Use Iff to avoid nil pointer dereference when user.Avatar is nil
Iff(user.Avatar != nil, func() Node {
return Img(Src(user.Avatar.URL))
}),
)
}Grouping Without a Parent Element
Use to group multiple nodes without wrapping them in a parent element:
Groupgo
func FormFields(required bool) Node {
return Group{
Label(For("email"), Text("Email")),
Input(Type("email"), ID("email")),
If(required, Span(Class("required"), Text("*"))),
}
}List Rendering
go
func TodoList(todos []Todo) Node {
return Ul(Class("todo-list"),
Map(todos, func(t Todo) Node {
return Li(Text(t.Title))
}),
)
}HTML Document
go
func Page(title string, body Node) Node {
return HTML5(HTML5Props{
Title: title,
Language: "en",
Head: []Node{
Link(Rel("stylesheet"), Href("/styles.css")),
},
Body: []Node{body},
})
}HTTP Handler
go
import ghttp "maragu.dev/gomponents/http"
func HomeHandler(w http.ResponseWriter, r *http.Request) (Node, error) {
return Page("My App",
Div(Class("container"),
H1(Text("Hello, World!")),
),
), nil
}
// In main:
http.HandleFunc("/", ghttp.Adapt(HomeHandler))The package provides:
http- type - function signature that returns
Handler(Node, error) - - converts Handler to
Adapt(Handler)http.HandlerFunc - Error handling with custom status codes via interface
StatusCode() int