Skip to content

Commit

Permalink
README: major rewrite, with embedded example
Browse files Browse the repository at this point in the history
Reconstruct README to better promote the project.
  • Loading branch information
dolmen committed Mar 28, 2024
1 parent 02092b8 commit bf367b5
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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é
Expand Down

0 comments on commit bf367b5

Please sign in to comment.