GithubHelp home page GithubHelp logo

codegen's Introduction

Codegen

GoDoc Widget Build Status codecov Go Report Card

Helpers fo generating go codes

Example

package main

import (
	"github.com/go-courier/codegen"
)

func main()  {
	file := codegen.NewFile("main", "examples/range-and-close/range-and-close.go")
 
    file.WriteBlock(
        codegen.Func(codegen.Var(codegen.Int, "n"), codegen.Var(codegen.Chan(codegen.Int), "c"), ).Named("fibonacci").Do(
            file.Expr("x, y := 0, 1"),
            codegen.For(file.Expr("i := 0"), file.Expr("i < n"), file.Expr("i++")).Do(
                file.Expr("c <- x"),
                file.Expr("x, y = y, x+y"),
            ),
            codegen.Call("close", codegen.Id("c")),
        ),
        codegen.Func().Named("main").Do(
            codegen.Define(codegen.Id("c")).By(codegen.Call("make", codegen.Chan(codegen.Int), file.Val(10))),
            codegen.Call("fibonacci", codegen.Call("cap", codegen.Id("c")), codegen.Id("c")).AsGo(),
            codegen.ForRange(codegen.Id("c"), "i").Do(
                codegen.Call(file.Use("fmt", "Println"), codegen.Id("i")),
            ),
        ),
    )
    
    file.WriteFile()
}

will generate file examples/range-and-close/range-and-close.go

package main

import (
	fmt "fmt"
)

func fibonacci(n int, c chan int) {
	x, y := 0, 1
	for i := 0; i < n; i++ {
		c <- x
		x, y = y, x+y
	}
	close(c)
}
func main() {
	c := make(chan int, 10)
	go fibonacci(cap(c), c)
	for i := range c {
		fmt.Println(i)
	}
}

codegen's People

Contributors

morlay avatar renovate-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

siskinc

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.