General Golang Code Generator

Build Status Go dev License matrix

gg

gg is a General Golang Code Generator: A Good Game to play with Golang.

package main

import (
	"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)

package main

import "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: Additional syntax, Steep learning curve, Complex logic is difficult to maintain
  • dave/jennifer: Overly abstract APIs, user need to take care about (), , everywhere.
  • 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

f := Group()
f.AddPackage("main")
// package main

Imports

f := Group()
f.NewImport().
    AddPath("context").
	AddDot("math").
	AddBlank("time").
	AddAlias("x", "testing")
// import (
//      "context"
//      . "math"
//      _ "time"
//      x "testing"
// )

Function

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

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, I borrowed most ideas and some code from it. Nice work!
Owner
Xuanwo
An infrastructure engineer.
Xuanwo
Similar Resources

Autoname - Friendly names generator with golang

autoname Friendly names generator inspired by moby's work. import "github.com/ci

Oct 16, 2022

Go-sudoku - Sudoku generator and solver implementation in GoLang

go-sudoku An implementation of Sudoku generators and solvers in GoLang. Usage He

Nov 7, 2022

Random fake data and struct generator for Go.

Faker Random fake data and struct generator for Go. More than 100 generator functions Struct generator Unique data generator Builtin types support Eas

Oct 3, 2022

Random fake data generator written in go

Random fake data generator written in go

Gofakeit Random data generator written in go Features 160+ Functions!!! Concurrent Global Rand Struct Generator Custom Functions Http Server Command L

Jan 1, 2023

A distributed unique ID generator of using Sonyflake and encoded by Base58

Indigo About A distributed unique ID generator of using Sonyflake and encoded by Base58. ID max length is 11 characters by unsigned int64 max value. A

Nov 24, 2022

Unit tests generator for Go programming language

Unit tests generator for Go programming language

GoUnit GoUnit is a commandline tool that generates tests stubs based on source function or method signature. There are plugins for Vim Emacs Atom Subl

Jan 1, 2023

GObject-introspection based bindings generator

WARNING! This project is no longer maintained. Probably doesn't even compile. GObject-introspection based bindings generator for Go. Work in progress

Jan 5, 2022

Typo/error resilient, human-readable token generator

toktok A human-friendly token generator Creates tokens which avoid characters that can be easily misinterpreted, like '1' and 'I' or '8' and 'B', as w

Sep 16, 2022

Fast and secure initramfs generator

Fast and secure initramfs generator

Booster - fast and secure initramfs generator Initramfs is a specially crafted small root filesystem that mounted at the early stages of Linux OS boot

Dec 28, 2022
Comments
  • 不支持导入包的别名吗

    不支持导入包的别名吗

    您好呀,我遇到了一个麻烦,希望帮我解答疑惑。 我使用如下语句为包设置一个别名时,得到一个导入错误的提示。 导入语句:

    f.NewImport().AddPath("myResponse \"http_server/response\"")

    错误信息: panic: 8:8: invalid import path: "myResponse \"http_server/response\""

    包含空格,或者转义字符时,会报错。

A general-purpose Cadence contract for trading NFTs on Flow

NFT Storefront The NFT storefront is a general-purpose Cadence contract for trading NFTs on Flow. NFTStorefront uses modern Cadence run-time type faci

Dec 24, 2022
XSD (XML Schema Definition) parser and Go/C/Java/Rust/TypeScript code generator

xgen Introduction xgen is a library written in pure Go providing a set of functions that allow you to parse XSD (XML schema definition) files. This li

Jan 1, 2023
Jennifer is a code generator for Go

Jennifer Jennifer is a code generator for Go. package main import ( "fmt" . "github.com/dave/jennifer/jen" ) func main() { f := NewFile("m

Dec 25, 2022
Default godoc generator - make your first steps towards better code documentation

godoc-generate Overview godoc-generate is a simple command line tool that generates default godoc comments on all exported types, functions, consts an

Sep 14, 2022
Protocol Buffers to HTTP client code generator/converter

Proto2http Proto2http provides a code generation tool to convert your protocol buffers (.proto) files into invokable HTTP request. Usage proto2http -p

Oct 23, 2022
The High Code Framework (low-code for devs)

hof - the high code framework The hof tool tries to remove redundent development activities by using high level designs, code generation, and diff3 wh

Dec 24, 2022
🎄 My code for the Advent of Code of year 2021 in Go.

Advent of Code 2021 This repository contains all code that I wrote for the Advent of Code 2021. This year I chose to try and learn Go. Enjoy! Built wi

Dec 9, 2021
:guardsman: A teeny tiny and somewhat opinionated generator for your next golang project

A Yeoman Golang Generator We are very sorry Gophers, but other names for the generator where taken, so we choose go-lang. But we have gocreate as an a

Sep 27, 2022
RinkWorks fantasy name generator in golang

RinkWorks fantasy name generator in golang

Dec 25, 2022
Go Param Generator for golang

Go Param Generator Overview Generates struct's for you. No need to write it by hands Adds Getters and Setters Adds Constructor Easy to use Usage gopar

Dec 13, 2021