Skip to content

Yet another code generator for golang; generics included

License

Notifications You must be signed in to change notification settings

nchern/go-codegen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Report Card Coverage

go-codegen

With a lack of templates some of the gophers really miss them... Yet another attempt of code generaton in Golang.

Testing

make test # generates code and runs tests

Install

make install

Usage

# Prints help
$ go-codegen -h

Generics

Usage

# Outputs built-in string to int map implementation to stdout
go-codegen generic -t=hashmap string int

# Reads generic source from file pkg/generic/list/list.go and outputs typed implementation to stdout
go-codegen generic -f pkg/generic/list/list.go string

# More help on available built-ins and other command line params
go-codegen help generic

Generics are implemented by parsing an input go source file into an AST and substituting predefined "generic" types. Currently only a fixed list of such types is supported: T0, T1, ..., T5. The advantage of such approach as the generic implementation(input source) is the correct go source that can be tested. See build-ins as examples of how to define a generic type. See test code for more examples.

Immutables

Usage

# Gets a .go source file with defined interfaces and outputs implementation along with builder class to create instances
go-codegen --pkg=model immutable -f tests/immutable/model/model.go

An experiment. Inspired by Java Immutables. See more examples in test code.

Constructor

Generates constructor function for struct. More details see here.

Impl

Generates interface implementations stubs. More details see here.

Nice to have

  • Integration with gen