GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - goplus/gogen: Code generator for the Go language

BranchesTags
Open more actions menu

Latest commit

 

History

1,923 Commits
1,923 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
chore
chore
 
 
internal
internal
 
 
packages
packages
 
 
target
target
 
 
token
token
 
 
tutorial/01-Basic
tutorial/01-Basic
 
 
typeutil
typeutil
 
 
.gitignore
.gitignore
 
 
CLAUDE.md
CLAUDE.md
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
ast.go
ast.go
 
 
ast_test.go
ast_test.go
 
 
builtin.go
builtin.go
 
 
builtin_gengo.go
builtin_gengo.go
 
 
builtin_genjs.go
builtin_genjs.go
 
 
builtin_test.go
builtin_test.go
 
 
cb_unit.go
cb_unit.go
 
 
cb_unit_test.go
cb_unit_test.go
 
 
codebuild.go
codebuild.go
 
 
codebuild_test.go
codebuild_test.go
 
 
constraint.go
constraint.go
 
 
error_msg_test.go
error_msg_test.go
 
 
func.go
func.go
 
 
func_ext.go
func_ext.go
 
 
go.mod
go.mod
 
 
gogen_test.go
gogen_test.go
 
 
gogenjs_test.go
gogenjs_test.go
 
 
gow.go
gow.go
 
 
gow_genjs.go
gow_genjs.go
 
 
import.go
import.go
 
 
line_directive_test.go
line_directive_test.go
 
 
package.go
package.go
 
 
package_test.go
package_test.go
 
 
param_go125.go
param_go125.go
 
 
param_legacy.go
param_legacy.go
 
 
stmt.go
stmt.go
 
 
template.go
template.go
 
 
type_ext.go
type_ext.go
 
 
type_tuple.go
type_tuple.go
 
 
type_tuple_test.go
type_tuple_test.go
 
 
type_var_and_const.go
type_var_and_const.go
 
 
typeparams.go
typeparams.go
 
 
typeparams_test.go
typeparams_test.go
 
 
typesinfer.go
typesinfer.go
 
 
unsafe_test.go
unsafe_test.go
 
 
util_gengo.go
util_gengo.go
 
 
util_genjs.go
util_genjs.go
 
 
utilast_gengo.go
utilast_gengo.go
 
 
utilast_genjs.go
utilast_genjs.go
 
 
utilast_test.go
utilast_test.go
 
 
validtype.go
validtype.go
 
 
xgo_test.go
xgo_test.go
 
 

Repository files navigation

gogen - Code generator for the Go language

Build Status GitHub release Coverage Status GoDoc

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:

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], false) // 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.TyAny, "x", "y").                // var x, y any
		Val(fmt.Ref("Println")).
		/**/ VarVal("a").VarVal("b").VarVal("c"). // fmt.Println(a, b, c)
		/**/ Call(3).EndStmt().
		NewClosure(types.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:

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"

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

About

Code generator for the Go language

Topics

code-generatorgolanggoplusstem-education

Resources

Readme
Apache-2.0 license
Activity
Custom properties

Stars

171 stars

Watchers

3 watching

Forks

36 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.