diff --git a/README.md b/README.md index f0f7788..9ce4b7e 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,39 @@ [![Go Report Card](https://goreportcard.com/badge/github.com/dolmen-go/jsonptr)](https://goreportcard.com/report/github.com/dolmen-go/codegen) -Utilities for writing code generators from a [text template](https://golang.org/pkg/text/template/), -with output properly formatted thanks to [`gofmt`](https://golang.org/pkg/go/format/). +Package `codegen` provides a Go API for writing Go code generators from a [`text/template`](https://pkg.go.dev/text/template). + +```go +const tmpl = `// Code generated by main.go; DO NOT EDIT. + +package main + +const x = {{ printf "%q" .str }} + +var v = {{ printf "%#v" .array }} +` + +func main() { + codegen.MustParse(tmpl).CreateFile( + "main_gen.go", + map[string]any{ + "str": `abcdef`, + "array": []int{1, 2, 3}, + }, + ) +} +``` + +[Try on the Go Playground](https://go.dev/play/p/CfJBt9ng-4w). + +Features: +* The Go syntax is validated before saving. +* The output properly is reformatted thanks to [`gofmt`](https://pkg.go.dev/go/format). +* The Go standard for marking generated code (https://golang.org/s/generatedcode) is enforces. See [documentation](https://godoc.org/github.com/dolmen-go/codegen) and [example](example_test.go). + ## License Copyright 2016-2024 Olivier Mengué