# gomponents: HTML Components in Pure Go - LLM Documentation
## Overview
gomponents is a Go library that enables building HTML components using pure Go code. Instead of using traditional HTML templates, developers write HTML as Go functions that compile to type-safe, performant HTML5 output. This approach leverages Go's type system, IDE support, and debugging capabilities while avoiding template language complexity.
## Core Concepts
### Node Interface
The fundamental building block is the `Node` interface:
```go
type Node interface {
Render(w io.Writer) error
}
```
Everything in gomponents implements this interface - elements, attributes, text, and components.
### Node Types
- **ElementType**: Regular HTML elements (div, span, etc.) and text nodes
- **AttributeType**: HTML attributes (class, href, etc.)
The library automatically handles proper placement during rendering.
## Installation
```bash
go get maragu.dev/gomponents
```
## Import Patterns
### Dot imports (recommended):
Contrary to common idiomatic Go, dot imports are the recommended approach for gomponents as they make the code read like a DSL for HTML:
```go
import (
. "maragu.dev/gomponents"
. "maragu.dev/gomponents/html"
. "maragu.dev/gomponents/components"
)
```
### Standard imports with aliases (alternative):
For those who prefer avoiding dot imports, use single-letter aliases:
```go
import (
g "maragu.dev/gomponents"
h "maragu.dev/gomponents/html"
c "maragu.dev/gomponents/components"
ghttp "maragu.dev/gomponents/http"
)
```
## Package Structure
### maragu.dev/gomponents (core)
Core interfaces and helper functions:
- `Node` interface
- `El(name string, children ...Node)` - create custom elements
- `Attr(name string, value ...string)` - create custom attributes
- `Text(string)` - HTML-escaped text
- `Textf(format string, args...)` - formatted escaped text
- `Raw(string)` - unescaped HTML
- `Rawf(format string, args...)` - formatted unescaped HTML
- `Group([]Node)` - group multiple nodes
- `Map[T]([]T, func(T) Node)` - transform slices to nodes
- `If(condition bool, node Node)` - conditional rendering
- `Iff(condition bool, func() Node)` - lazy conditional rendering
### maragu.dev/gomponents/html
All HTML5 elements and attributes as Go functions:
- Elements: `Div()`, `Span()`, `A()`, `H1()`, etc.
- Attributes: `Class()`, `ID()`, `Href()`, `Style()`, etc.
- Special: `Doctype()` for HTML5 doctype declaration
### maragu.dev/gomponents/components
Higher-level components:
- `HTML5(HTML5Props)` - complete HTML5 document structure
- `Classes` - dynamic class management map
### maragu.dev/gomponents/http
HTTP handler integration:
- `Handler` type - returns (Node, error)
- `Adapt()` - converts Handler to http.HandlerFunc
## Basic Usage Examples
### Simple Element
```go
import (
. "maragu.dev/gomponents"
. "maragu.dev/gomponents/html"
)
//
```
### Rendering to Buffer
Test component output:
```go
import (
"bytes"
)
var buf bytes.Buffer
err := node.Render(&buf)
html := buf.String()
```
## Performance Considerations
1. **Direct Rendering**: Nodes render directly to io.Writer without intermediate string allocation
2. **No Reflection**: Pure function calls, no runtime reflection overhead
3. **Compile-Time Safety**: Errors caught at compile time, not runtime
4. **Zero Dependencies**: Core library has no external dependencies
## Common Gotchas
1. **Nil Nodes**: Nil nodes are safely ignored during rendering
2. **Attribute Order**: Attributes render in the order they're specified
3. **Escaping**: Use Text() for escaped content, Raw() for unescaped HTML
4. **Void Elements**: Children (except attributes) are ignored for void elements
## Summary
gomponents provides a type-safe, performant way to generate HTML in Go applications. It's particularly well-suited for:
- Server-side rendered web applications
- API servers that return HTML
- Static site generators
- Email template generation
- Any scenario where you need programmatic HTML generation with Go's type safety
The library's philosophy emphasizes simplicity, type safety, and Go idioms over template languages, making it an excellent choice for Go developers who prefer staying within the Go ecosystem.