Refine the template to fix excessive newlines

This commit is contained in:
2024-09-12 00:40:20 +02:00
parent 2e10e11ab9
commit 440a6e33e6
4 changed files with 67 additions and 22 deletions

39
main.go
View File

@@ -30,6 +30,12 @@ func init() {
log.Lmicroseconds|log.Lshortfile)
}
var fns = template.FuncMap{
"plus1": func(x int) int {
return x + 1
},
}
func main() {
flag.Parse()
files := flag.Args()
@@ -49,25 +55,42 @@ func main() {
{
Name: "test",
Params: []Param{
{Name: "a", Type: "int", Comment: "test"},
{Name: "b", Type: "string", Comment: "test"},
{Name: "a", Type: "int", Comment: "test param 1"},
{Name: "b", Type: "string", Comment: "test param 2"},
},
Returns: []Return{
{Type: "int", Comment: "test"},
{Type: "string", Comment: "test"},
{Type: "int", Comment: "test return int"},
{Type: "string", Comment: "test return string"},
},
Comment: "test",
Comment: "test method",
},
{
Name: "test2",
Params: []Param{
{Name: "a", Type: "int", Comment: "test param 1"},
{Name: "b", Type: "string", Comment: "test param 2"},
},
Returns: []Return{
{Type: "int", Comment: "test return int"},
{Type: "string", Comment: "test return string"},
},
Comment: "test method",
},
},
}
ltemplate, err := template.New("class").Parse(templatestr)
ltemplate, err := template.New("class").Funcs(fns).Parse(templatestr)
if err != nil {
Error.Printf("Error parsing template: %v", err)
return
}
log.Printf("%#v", ltemplate)
ltemplate.Execute(os.Stdout, test)
outfile, err := test.GetOutFile()
if err != nil {
Error.Printf("Error creating output file: %v", err)
return
}
ltemplate.Execute(outfile, test)
for _, file := range files {
fmt.Println(file)