Skip to content
Resource Construction

Resource Construction

The gofhir/models library provides three distinct patterns for creating FHIR resources. All three produce the same Go struct types, so you can mix and match patterns within a single project. Choose the one that best fits your coding style and use case.

Overview

PatternBest ForVerbosityCompile-time Safety
Struct LiteralsFull control, one-shot initializationMedium (requires pointers)Full
Builder PatternStep-by-step construction, fluent chainsLowFull

Both patterns set fields on the same underlying struct (for example, r4.Patient), so serialization and deserialization work identically regardless of how the resource was created.

Quick Comparison

Struct Literal

active := true
patient := r4.Patient{
    Id:           &id,
    Active:       &active,
}

Builder Pattern

patient := r4.NewPatientBuilder().
    SetId("patient-1").
    SetActive(true).
    Build()

Guides

Last updated on