# Go Patterns

> Patrones de diseño idiomáticos en Go. Usar al elegir patrones para resolver problemas o diseñar sistemas extensibles. Use when this capability is needed.

- Skill: `tomevault-io/go-patterns-2` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add tomevault-io/go-patterns-2`
- Raw SKILL.md: https://api.skillmd.com/api/skills/tomevault-io/go-patterns-2/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: tomevault-io (https://skillmd.com/u/tomevault-io)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/tomevault-io/go-patterns-2

---


# Skill: go-patterns

Patrones de diseño en Go con ejemplos del proyecto GoFHIR.

## Patrones Implementados en GoFHIR

### 1. Pipeline Pattern (validator/pipeline.go)

```go
// Interface para cada fase
type PhaseValidator interface {
    Name() string
    Priority() int
    Validate(ctx context.Context, resource []byte, result *ValidationResult) error
}

// Pipeline ejecuta fases en orden
type pipeline struct {
    phases []PhaseValidator
}

func (p *pipeline) Execute(ctx context.Context, resource []byte) (*ValidationResult, error) {
    result := NewValidationResult()
    for _, phase := range p.phases {
        if err := phase.Validate(ctx, resource, result); err != nil {
            return result, err
        }
    }
    return result, nil
}

// Fases: Structure, Constraints, Terminology, Extensions, References
```

### 2. Registry Pattern (fhirpath/funcs/registry.go)

```go
type Registry struct {
    mu    sync.RWMutex
    funcs map[string]*FuncDef
}

func (r *Registry) Register(def *FuncDef) {
    r.mu.Lock()
    defer r.mu.Unlock()
    r.funcs[def.Name] = def
}

func (r *Registry) Get(name string) (*FuncDef, bool) {
    r.mu.RLock()
    defer r.mu.RUnlock()
    return r.funcs[name]
}
```

### 3. Visitor Pattern (validator/treewalker.go)

```go
type NodeVisitor interface {
    Visit(path string, value interface{}) error
}

type TreeWalker struct {
    visitor NodeVisitor
}

func (tw *TreeWalker) Walk(resource map[string]interface{}) error {
    return tw.walkNode("", resource)
}
```

### 4. Composite Pattern (validator/terminology.go)

```go
type CompositeTerminologyService struct {
    services []TerminologyService
}

func (c *CompositeTerminologyService) ValidateCode(ctx context.Context, system, code, url string) (bool, error) {
    for _, svc := range c.services {
        if valid, err := svc.ValidateCode(ctx, system, code, url); err == nil && valid {
            return true, nil
        }
    }
    return false, nil
}
```

### 5. Object Pool (fhirpath/types/pool.go)

```go
var collectionPool = sync.Pool{
    New: func() interface{} { return make(Collection, 0, 8) },
}

func GetCollection() Collection {
    return collectionPool.Get().(Collection)[:0]
}

func PutCollection(c Collection) {
    collectionPool.Put(c[:0])
}
```

### 6. LRU Cache (fhirpath/cache.go)

```go
type ExpressionCache struct {
    mu    sync.RWMutex
    cache map[string]*Expression
    limit int
}
```

## Cuándo Usar

| Patrón | Usar cuando... |
|--------|----------------|
| Pipeline | Procesos multi-fase (validación) |
| Registry | Registro dinámico (funciones FHIRPath) |
| Visitor | Recorrer estructuras (JSON FHIR) |
| Composite | Combinar servicios (terminology) |
| Object Pool | Reducir GC (collections) |
| LRU Cache | Cachear resultados costosos |

## Referencias

```text
validator/pipeline.go, phase.go, phases.go
validator/treewalker.go
validator/terminology.go
fhirpath/funcs/registry.go
fhirpath/types/pool.go
fhirpath/cache.go
```

---
> Converted and distributed by [TomeVault](https://tomevault.io/claim/gofhir) — claim your Tome and manage your conversions.
<!-- tomevault:4.0:skill_md:2026-04-13 -->

