Building a static site generator in Go requires parsing templates, reading source files, and writing output HTML. Use the html/template package to define layouts and the os package to handle file I/O.
package main
import (
"html/template"
"os"
"path/filepath"
)
func main() {
tmpl := template.Must(template.ParseFiles("templates/layout.html"))
files, _ := filepath.Glob("content/*.md")
for _, file := range files {
outFile := filepath.Join("public", filepath.Base(file)+".html")
os.WriteFile(outFile, []byte("<html>...</html>"), 0644)
}
}
- Create a
templatesdirectory and add alayout.htmlfile with your HTML structure. - Create a
contentdirectory and add your.mdsource files. - Run the Go program to parse templates and generate static HTML files in the
publicdirectory.