/gox

Code generator for the Go language

Primary LanguageGoApache License 2.0Apache-2.0

gox - Code generator for the Go language

Build Status Go Report Card GitHub release Coverage Status GoDoc

Quick start

Create a file named hellogen.go, like below:

package main

import (
	"go/token"
	"go/types"
	"os"

	"github.com/goplus/gox"
	"github.com/goplus/gox/packages"
)

func ctxRef(pkg *gox.Package, name string) gox.Ref {
	return pkg.CB().Scope().Lookup(name)
}

func main() {
	imp, _, _ := packages.NewImporter(nil, "fmt", "strings", "strconv")
	pkg := gox.NewPackage("", "main", &gox.Config{Importer: imp})
	fmt := pkg.Import("fmt")
	v := pkg.NewParam(token.NoPos, "v", types.Typ[types.String]) // v string

	pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
		DefineVarStart(token.NoPos, "a", "b").Val("Hi").Val(3).EndInit(2). // a, b := "Hi", 3
		NewVarStart(nil, "c").Val(ctxRef(pkg, "b")).EndInit(1).            // var c = b
		NewVar(gox.TyEmptyInterface, "x", "y").                            // var x, y interface{}
		Val(fmt.Ref("Println")).
		/**/ Val(ctxRef(pkg, "a")).Val(ctxRef(pkg, "b")).Val(ctxRef(pkg, "c")). // fmt.Println(a, b, c)
		/**/ Call(3).EndStmt().
		NewClosure(gox.NewTuple(v), nil, false).BodyStart(pkg).
		/**/ Val(fmt.Ref("Println")).Val(v).Call(1).EndStmt(). // fmt.Println(v)
		/**/ End().
		Val("Hello").Call(1).EndStmt(). // func(v string) { ... } ("Hello")
		End()

	gox.WriteTo(os.Stdout, pkg, false)
}

Try it like:

go mod init hello
go mod tidy
go run hellogen.go

This will dump Go source code to stdout. The following is the output content:

package main

import fmt "fmt"

func main() {
	a, b := "Hi", 3
	var c = b
	var x, y interface {
	}
	fmt.Println(a, b, c)
	func(v string) {
		fmt.Println(v)
	}("Hello")
}