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
- Host: GitHub
- URL: https://github.com/dolmen-go/codegen
- Owner: dolmen-go
- License: apache-2.0
- Created: 2016-10-06T22:18:23.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2025-03-04T13:49:06.000Z (over 1 year ago)
- Last Synced: 2025-03-23T23:36:13.270Z (over 1 year ago)
- Topics: code-generation, go, golang-package, hacktoberfest-accepted
- Language: Go
- Homepage: https://pkg.go.dev/github.com/dolmen-go/codegen
- Size: 33.2 KB
- Stars: 13
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# codegen - Utilities for Go code generators
[](https://pkg.go.dev/github.com/dolmen-go/codegen)
[](https://codecov.io/gh/dolmen-go/codegen)
[](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.