generator_helpers
is a Go package designed to provide utility functions for common operations such as template generation, YAML processing, and error handling in Go applications.
To use generator_helpers
in your Go project, you need to install it as a dependency:
go get github.com/point-c/generator_helpers
Below are some examples of how to use the generator_helpers
package:
To unmarshal YAML data from a file:
var config MyConfig
err := generator_helpers.UnmarshalYAML("config.yaml", &config)
if err != nil {
// Handle error
}
Generating text or HTML from templates:
tmpl, err := generator_helpers.NewTemplate[*text_template.Template](templateFS, funcs)
if err != nil {
// Handle error
}
var data MyData
generator_helpers.Generate(tmpl, data, "templateName", "output.txt")
Simplifying error handling with Must
and Check
:
getValue := func() (int, error) { return 1, errors.New("error") }
value := generator_helpers.Must(getValue())
generator_helpers.Check(err)
The generator_helpers
package offers functionalities to format Go source code. Below is an example of how to use these functions:
To format a Go source code byte slice:
sourceCode := []byte("package main\nimport \"fmt\"\nfunc main() {fmt.Println(\"hello world\")}")
formattedCode, err := generator_helpers.GoFmt(sourceCode)
if err != nil {
// Handle formatting error
}
// Use formattedCode...
If you have Go source code in an io.Reader
, you can format it as follows:
var reader io.Reader = ... // your io.Reader with Go source code
formattedCode, err := generator_helpers.GoFmtReader(reader)
if err != nil {
// Handle formatting error
}
// Use formattedCode...
The package includes tests that demonstrate its functionality. Use Go's testing tools to run the tests:
go test
To regenerate godocs:
go generate -tags docs ./...