https://github.com/visualfc/gox
Code generator for the Go language
https://github.com/visualfc/gox
Last synced: 3 months ago
JSON representation
Code generator for the Go language
- Host: GitHub
- URL: https://github.com/visualfc/gox
- Owner: visualfc
- License: apache-2.0
- Fork: true (goplus/gogen)
- Created: 2021-07-02T01:21:37.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2025-09-19T07:58:42.000Z (7 months ago)
- Last Synced: 2025-09-19T09:45:50.855Z (7 months ago)
- Language: Go
- Size: 1.37 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
gogen - Code generator for the Go language
========
[](https://github.com/goplus/gogen/actions/workflows/go.yml)
[](https://goreportcard.com/report/github.com/goplus/gogen)
[](https://github.com/goplus/gogen/releases)
[](https://codecov.io/gh/goplus/gogen)
[](https://pkg.go.dev/github.com/goplus/gogen)
`gogen` is a general-purpose Go code generation toolkit with zero third-party dependencies. Like the Go compiler, It can perform type checking of expressions. For example, if you generate an expression like `"Hello" + 1`, it will report the corresponding error.
## Quick start
Create a file named `hellogen.go`, like below:
```go
package main
import (
"go/token"
"go/types"
"os"
"github.com/goplus/gogen"
)
func main() {
pkg := gogen.NewPackage("", "main", nil)
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").VarVal("b").EndInit(1). // var c = b
NewVar(gogen.TyEmptyInterface, "x", "y"). // var x, y interface{}
Val(fmt.Ref("Println")).
/**/ VarVal("a").VarVal("b").VarVal("c"). // fmt.Println(a, b, c)
/**/ Call(3).EndStmt().
NewClosure(gogen.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()
pkg.WriteTo(os.Stdout)
}
```
Try it like:
```shell
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:
```go
package main
import "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")
}
```