An open API service indexing awesome lists of open source software.

https://github.com/dolmen-go/codegen

Utilities to write Go code generators from text templates
https://github.com/dolmen-go/codegen

code-generation go golang-package hacktoberfest-accepted

Last synced: about 1 year ago
JSON representation

Utilities to write Go code generators from text templates

Awesome Lists containing this project

README

          

# codegen - Utilities for Go code generators

[![Go Reference](https://pkg.go.dev/badge/github.com/dolmen-go/codegen.svg)](https://pkg.go.dev/github.com/dolmen-go/codegen)
[![Codecov](https://codecov.io/gh/dolmen-go/codegen/graph/badge.svg?token=y7mxhgwKi7)](https://codecov.io/gh/dolmen-go/codegen)
[![Go Report Card](https://goreportcard.com/badge/github.com/dolmen-go/jsonptr)](https://goreportcard.com/report/github.com/dolmen-go/codegen)

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é

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.