Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Xuanwo/gg
General Golang Code Generator
https://github.com/Xuanwo/gg
code-generation codegen golang-library
Last synced: 3 months 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 (about 3 years ago)
- Default Branch: master
- Last Pushed: 2023-06-23T15:19:32.000Z (over 1 year ago)
- Last Synced: 2024-06-20T15:02:23.933Z (5 months ago)
- Topics: code-generation, codegen, golang-library
- Language: Go
- Homepage:
- Size: 55.7 KB
- Stars: 188
- Watchers: 4
- Forks: 14
- 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
[![Build Status](https://github.com/Xuanwo/gg/workflows/Unit%20Test/badge.svg?branch=master)](https://github.com/Xuanwo/gg/actions?query=workflow%3A%22Unit+Test%22)
[![Go dev](https://pkg.go.dev/badge/github.com/Xuanwo/gg)](https://pkg.go.dev/github.com/Xuanwo/gg)
[![License](https://img.shields.io/badge/license-apache%20v2-blue.svg)](https://github.com/Xuanwo/gg/blob/master/LICENSE)
[![matrix](https://img.shields.io/matrix/xuanwo@gg:matrix.org.svg?logo=matrix)](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!