Examples
Examples
This section provides practical, runnable Go code examples that demonstrate how to use the gofhir/models library in real-world scenarios. The examples progress from common construction patterns to full production use cases.
Topics
Building patients, observations, bundles, and working with CodeableConcept, Coding, and the helpers package.
Production patterns including HTTP handlers, API response parsing, format conversion, and resource routing.
Quick Start
If you are new to the library, here is the simplest possible example to get started:
package main
import (
"fmt"
"github.com/gofhir/models/r4/v2"
)
func main() {
// Create a Patient using the builder
patient := r4.NewPatientBuilder().
SetId("hello-fhir").
SetActive(true).
AddName(r4.HumanName{
Family: ptrTo("World"),
Given: r4.PtrSlice("Hello"),
}).
Build()
// Serialize to JSON
data, _ := r4.Marshal(patient)
fmt.Println(string(data))
}
func ptrTo[T any](v T) *T {
return &v
}Output:
{
"resourceType": "Patient",
"id": "hello-fhir",
"active": true,
"name": [{"family": "World", "given": ["Hello"]}]
}All examples in this section use the r4 package. The same patterns apply to r4b and r5.
Last updated on