Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/point-c/generator-helpers
Utility functions for Go code generation
https://github.com/point-c/generator-helpers
Last synced: about 2 months ago
JSON representation
Utility functions for Go code generation
- Host: GitHub
- URL: https://github.com/point-c/generator-helpers
- Owner: point-c
- License: mit
- Created: 2024-01-04T03:38:03.000Z (almost 1 year ago)
- Default Branch: main
- Last Pushed: 2024-01-20T00:38:30.000Z (12 months ago)
- Last Synced: 2024-01-20T02:03:59.994Z (12 months ago)
- Language: Go
- Homepage: https://point-c.github.io/generator-helpers/
- Size: 350 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# generator_helpers
[![Go Reference](https://img.shields.io/badge/godoc-reference-%23007d9c.svg)](https://point-c.github.io/generator-helpers)
## Overview
`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.
## Installation
To use `generator_helpers` in your Go project, you need to install it as a dependency:
```bash
go get github.com/point-c/generator_helpers
```## Usage
Below are some examples of how to use the `generator_helpers` package:
### Unmarshalling YAML Data
To unmarshal YAML data from a file:
```go
var config MyConfig
err := generator_helpers.UnmarshalYAML("config.yaml", &config)
if err != nil {
// Handle error
}
```### Template Generation
Generating text or HTML from templates:
```go
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")
```### Error Handling
Simplifying error handling with `Must` and `Check`:
```go
getValue := func() (int, error) { return 1, errors.New("error") }
value := generator_helpers.Must(getValue())
generator_helpers.Check(err)
```### Go Code Formatting
The `generator_helpers` package offers functionalities to format Go source code. Below is an example of how to use these functions:
#### Formatting Go Source Code
To format a Go source code byte slice:
```go
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...
```#### Formatting Source Code from an `io.Reader`
If you have Go source code in an `io.Reader`, you can format it as follows:
```go
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...
```## Testing
The package includes tests that demonstrate its functionality. Use Go's testing tools to run the tests:
```bash
go test
```## Godocs
To regenerate godocs:
```bash
go generate -tags docs ./...
```