On this half, I’ll go over every template for producing code.
The iBuilder template
This template shall be used to create the
iBuilder interface. This interface defines capabilities for setting the properties and getting the Product Goal.
The minuses simply earlier than the double ending brackets take away whitespace and newlines.
In strains 4–6, we loop over every property to create setter capabilities for the properties of the product goal.
In line 5, we use a conditional so as to add ‘Num’ earlier than the variable title if the variable sort is an ‘int’.
In strains 11–15, we loop over the Concrete Targets to create code that returns capabilities that implement the
The Concrete Targets template
processConcreteTargets() will name
processTemplate() for every of the Concrete Targets.
The result’s that the template under shall be rendered for every Concrete Goal that we’ve outlined. In our information, we’ve outlined two Concrete Targets —
Issues to notice:
- In line 3, we create an inside variable within the template, referred to as
- In strains 5–7, we loop over the outlined properties.
- In line 10, we use the
Sprigto Capitalize the primary letter of the
- In line 15, we use
if eqto render “Num” earlier than the variable title if the variable sort is an integer.
- In line 16, there’s a remark “// change this”. This remark is a message for the person of the generated code.
The Product Goal template
This template shall be rendered to supply the
struct for the Product Goal.
In strains 4–6, there’s a easy loop that generates all of the properties of the
The Director template
This template renders a Director. This Director will construct the Product Goal.
Issues to notice right here:
Within the strains 17,19,and 21, the
title operate from
Sprig is getting used, right here once more, to capitalize the primary letter of the variable string.
In strains 18–19, we loop over the properties.
In line 19, we once more use a conditional so as to add “Num” to the property whether it is an integer.
A Pattern Primary template
To ensure that the person of the generated code to get began with it, I’ve additionally made a template that renders a pattern
predominant file, referred to as
On this file, there are some examples of utilizing the Concrete Targets constructed with the Builder Design Sample.
In strains 6–8, we create many inside variables to be used within the template.
In strains 9–16, we loop over the Concrete Targets. Notice that we use the
$index and the
$merchandise inside the loop.
In line 11, the
$index is utilized in a conditional to find out which assertion ought to be rendered.
In strains 13–15, a nested loop is used to create “print” statements for every of the properties of the Concrete Targets.
Be certain that the principle program from the
codegen_builder.go file and the templates are in the identical listing. Then run
go run codegen_builder.go in your terminal. Now this system will generate code with the assistance of the templates and output it within the
Let’s go to that listing and take a look on the code that we generated.
We will check the generated code by operating
go run .within the terminal. Once we run that command, then we must always get an output within the terminal as follows:
This reveals that every one our generated code runs with out errors and works as meant.
If we have been really to make use of the generated code, we might nonetheless should edit the Concrete Targets recordsdata. See screenshots under: