https://github.com/Xuanwo/gg
General Golang Code Generator
https://github.com/Xuanwo/gg
code-generation codegen golang-library
Last synced: about 1 month ago
JSON representation
General Golang Code Generator
- Host: GitHub
- URL: https://github.com/Xuanwo/gg
- Owner: Xuanwo
- License: apache-2.0
- Created: 2021-08-27T07:40:12.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2023-06-23T15:19:32.000Z (almost 2 years ago)
- Last Synced: 2025-04-07T14:12:58.925Z (about 2 months ago)
- Topics: code-generation, codegen, golang-library
- Language: Go
- Homepage:
- Size: 55.7 KB
- Stars: 198
- Watchers: 3
- Forks: 15
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- go-awesome - gg - Go code generation (Open source library / Code Generation)
README
[](https://github.com/Xuanwo/gg/actions?query=workflow%3A%22Unit+Test%22)
[](https://pkg.go.dev/github.com/Xuanwo/gg)
[](https://github.com/Xuanwo/gg/blob/master/LICENSE)
[](https://matrix.to/#/#xuanwo@gg:matrix.org)# gg
`gg` is a General Golang Code Generator: A Good Game to play with Golang.
```go
package mainimport (
"fmt". "github.com/Xuanwo/gg"
)func main() {
f := NewGroup()
f.AddPackage("main")
f.NewImport().
AddPath("fmt")
f.NewFunction("main").AddBody(
String(`fmt.Println("%s")`, "Hello, World!"),
)
fmt.Println(f.String())
}
```Output (after `go fmt`)
```go
package mainimport "fmt"
func main() {
fmt.Println("Hello, World!")
}
```## Design
`gg` is a general golang code generator that designed for resolving problems exists in the following tools:
- [text/template](https://pkg.go.dev/text/template): Additional syntax, Steep learning curve, Complex logic is difficult to maintain
- [dave/jennifer](https://github.com/dave/jennifer): Overly abstract APIs, user need to take care about `()`, `,` everywhere.
- [kubernetes-sigs/kubebuilder](https://github.com/kubernetes-sigs/kubebuilder): Parse data from struct tags/comments, not a general code generator.In short, `gg` will provide near-native golang syntax and helpful API so play a good game with Golang. With `gg`, we can generate golang code more easily and understandable.
## Usage
### Package Name
```go
f := Group()
f.AddPackage("main")
// package main
```### Imports
```go
f := Group()
f.NewImport().
AddPath("context").
AddDot("math").
AddBlank("time").
AddAlias("x", "testing")
// import (
// "context"
// . "math"
// _ "time"
// x "testing"
// )
```### Function
```go
f := Group()
f.NewFunction("hello").
WithReceiver("v", "*World").
AddParameter("content", "string").
AddParameter("times", "int").
AddResult("v", "string").
AddBody(gg.String(`return fmt.Sprintf("say %s in %d times", content, times)`))
// func (v *World) hello(content string, times int) (v string) {
// return fmt.Sprintf("say %s in %d times", content, times)
//}
```### Struct
```go
f := Group()
f.NewStruct("World").
AddField("x", "int64").
AddField("y", "string")
// type World struct {
// x int64
// y string
//}
```## Acknowledgement
- `gg` is inspired by [dave/jennifer](https://github.com/dave/jennifer), I borrowed most ideas and some code from it. Nice work!