From e2b5cdc9d904220d1d1e24d1f4a145d0d08a259e Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Mon, 29 Sep 2025 19:59:05 +0100 Subject: [PATCH] Parsing data from YAML and outputting HTML --- go-templates/.gitignore | 2 ++ go-templates/build | 5 +++++ go-templates/data.yaml | 1 + go-templates/go.mod | 5 +++++ go-templates/go.sum | 3 +++ go-templates/main.go | 33 +++++++++++++++++++++++++++++++++ go-templates/tmpl/_.html | 1 + 7 files changed, 50 insertions(+) create mode 100644 go-templates/.gitignore create mode 100755 go-templates/build create mode 100644 go-templates/data.yaml create mode 100644 go-templates/go.mod create mode 100644 go-templates/go.sum create mode 100644 go-templates/main.go create mode 100644 go-templates/tmpl/_.html diff --git a/go-templates/.gitignore b/go-templates/.gitignore new file mode 100644 index 0000000..3091afd --- /dev/null +++ b/go-templates/.gitignore @@ -0,0 +1,2 @@ +/index.html +/out diff --git a/go-templates/build b/go-templates/build new file mode 100755 index 0000000..131ef4c --- /dev/null +++ b/go-templates/build @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +set -euo pipefail + +go build -o out main.go diff --git a/go-templates/data.yaml b/go-templates/data.yaml new file mode 100644 index 0000000..4976e00 --- /dev/null +++ b/go-templates/data.yaml @@ -0,0 +1 @@ +Name: Oliver Davies diff --git a/go-templates/go.mod b/go-templates/go.mod new file mode 100644 index 0000000..e11ee42 --- /dev/null +++ b/go-templates/go.mod @@ -0,0 +1,5 @@ +module go-templates + +go 1.24.4 + +require gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go-templates/go.sum b/go-templates/go.sum new file mode 100644 index 0000000..4bc0337 --- /dev/null +++ b/go-templates/go.sum @@ -0,0 +1,3 @@ +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/go-templates/main.go b/go-templates/main.go new file mode 100644 index 0000000..b012ddc --- /dev/null +++ b/go-templates/main.go @@ -0,0 +1,33 @@ +package main + +import ( + "log" + "os" + "text/template" + + "gopkg.in/yaml.v3" +) + +func check(err error) { + if err != nil { + log.Fatalf("error: %v", err) + } +} + +func main() { + out, err := os.Create("index.html") + check(err) + defer out.Close() + + data := map[string]any{} + + buf, err := os.ReadFile("data.yaml") + check(err) + err = yaml.Unmarshal(buf, &data) + check(err) + + t, err := template.ParseGlob("tmpl/*") + check(err) + err = t.Execute(out, data) + check(err) +} diff --git a/go-templates/tmpl/_.html b/go-templates/tmpl/_.html new file mode 100644 index 0000000..cc84aec --- /dev/null +++ b/go-templates/tmpl/_.html @@ -0,0 +1 @@ +

{{.Name}}