English | 简体中文
Builders use code generator to auto coding, coding to build a virtual world.
CodeGenerator read template files (*.tpl
by default) and generate code. It's just like a lightweight template compiler, which following the generation rules to generate several files.
We provide "package main" as a basic entry. You can build this (go build
) and execute the program.
But, it is more recommended to using this project as a library. You can realize any logic key or data loader fast and easy.
Several cases are using code generation tools to generate codes.
- django (web backend project)
- create-react-app (create react project)
- go-zero (generate microservice)
We explain this problem from the following two points - why we need to automatically generate code, and the difference between this library and other implementations.
- Reduce repetitive code writing
- Provide a set of code templates for development (microservices/web/app)
- Easy to maintain (sometimes only need to modify the template file)
Most code generation tools use the solution "template + hard code" to generate specific code. CodeGenerator generates code through a specific template language. It is not only satisfied with text generation and value replacement, but also supports complex logic such as loops / conditions.
- Quickly generate any project code, only need to follow the same set of rules
- It is very easy to increase or decrease logic(reserved word), almost all logic can be expanded, and fully realize your own logic
- You don't need to look at the logic, you can know all the file generation rules just by reading the template file
For example, by using the Loop
keyword to define loop printing in the template file, instead of writing infinite loop logic hard code in the generation tool
#{Loop 3}
hello world!
#{EndLoop}
- demo (basic rules)
- microservice (generate microservice, provide grpc service)
- k8s (generate kubernetes deployment files)
- ue4 (generate ue4 server plugin)
- proto (read .proto files as data)
- datetime (customize
DateTime
reserved word, which will print the current time)
- Using several DataLoader to load a key-value map (dictionary in code)
- Create a mission, it will read all template files, and every template will create a task to generate content.
- Print content to several files
Run the following command:
go get -u github.com/finishy1995/codegenerator