GithubHelp home page GithubHelp logo

gogen's Introduction

gogen

AST rewrite powered generics for Go

Usage

Somewhere in your code you need to define one or more generic types. Something like:

type I interface{}
type F interface{}

From here, you can write any code that uses those generic type (including tests):

func Add(left, right I) I {
    return left + right
}
func Sub(left, right F) F {
    return left - right
}

Using the gogen CLI you can then render the generic code with concrete types:

gogen -source github.com/username/package -destination . -rewrite "I=uint16" -rewrite "F=float64

The source code will be rewritten without the type I interface{} lines and with all references to generic types replaced with concrete types.

func Add(left, right uint16) uint16 {
    return left + right
}
func Sub(left, right float64) float64 {
    return left - right
}

The output will be formatted with gofmt (but not goimports).

More Options

In addition to direct rewrites, the CLI also offers the following options:

  • -unstar T

    This option triggers the rewrite of *T within the source to T before being rewritten as the concrete type. This is useful if rendering a generic as an interface type (such as `-rewrite "T=io.Reader"). The option may be given multiple times to unstar multiple generic types.

  • -rewrite "T=*int"

    If the concrete type given in a rewrite rule is prefixed with an * then a pointer type will be used. If the generic type is already a pointer then this could result in a double pointer to the concrete type. Use the unstar flag to prevent this. Any number of rewrites may be given per invocation.

  • -rewrite "T=pkg.Name"

    If the concrete type has a package prefix then the resulting code will correctly select the type from the target package. However, the rewrite will not change import statements or add new ones. It is recommended to use something like goimports to correct any missing or excess import statements after rewrites.

  • -package newname

    The package option causes the renderer to set a custom package name in all files rendered. By default, the original package name is reused.

License

This project is available under the Apache2.0 license. See the LICENSE file in this repository for the complete license text.

gogen's People

Contributors

kevinconway avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.