Parsing data from YAML and outputting HTML

This commit is contained in:
Oliver Davies 2025-09-29 19:59:05 +01:00
parent 37ad11a9b4
commit e2b5cdc9d9
7 changed files with 50 additions and 0 deletions

2
go-templates/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/index.html
/out

5
go-templates/build Executable file
View file

@ -0,0 +1,5 @@
#!/usr/bin/env bash
set -euo pipefail
go build -o out main.go

1
go-templates/data.yaml Normal file
View file

@ -0,0 +1 @@
Name: Oliver Davies

5
go-templates/go.mod Normal file
View file

@ -0,0 +1,5 @@
module go-templates
go 1.24.4
require gopkg.in/yaml.v3 v3.0.1 // indirect

3
go-templates/go.sum Normal file
View file

@ -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=

33
go-templates/main.go Normal file
View file

@ -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)
}

1
go-templates/tmpl/_.html Normal file
View file

@ -0,0 +1 @@
<h1>{{.Name}}</h1>