go语言提供了两种模版引擎,分别是html/template和text/template,两种模版引擎的语法相同,不同的地方就在于html中对标签等的处理。
模版语法参考连接:https://www.cnblogs.com/Pynix/p/4154630.html
我们这里使用text/template引擎做一个代码生成的小例子,选择生成一个java类的代码:
- 模版conf.template文件如下:
package {{ .PackageName }};
import java.util.Collections;
import java.util.Comparator;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
{{if ne .SuperClass ""}}import com.lehoo.sob.confsuper.{{.SuperClass}};{{end}}
/**
- excel|{{.ExcelName}}
- @author administrator
- 此类是系统自动生成类 不要直接修改,修改后也会被覆盖
*/
@JSONListener
{{if eq .SuperClass ""}}
public class Conf{{.SheetName}} {{"{"}}
{{else}}
public class Conf{{.SheetName}} extends {{.SuperClass}}{{"{"}}
{{end}}
/** 对应的数据文件 */
private static final String JSON_NAME = "Conf{{.SheetName}}.json";
/**索引*/
private static final String [] INDEXS = {{.Indexs}};
{{range .Props}}
/** {{.PNote}} */
private {{.PType}} {{.PName}};
{{end}}
/** 配置数据 */
private static Map{{"<"}}Object,Conf{{.SheetName}}{{">"}} datas = new LinkedHashMap{{"<"}}{{">"}}();
/**索引结构,加快查询速度*/
private static Map{{"<"}}String,Map{{"<"}}Object,List{{"<"}}Conf{{.SheetName}}{{">"}}{{">"}}{{">"}} indexs = new HashMap{{"<"}}{{">"}}();
/** 私有构造函数 */
private Conf{{.SheetName}}(){ }
/**初始化索引*/
private static void initIndex(){
//初始化索引结构
for(String index : INDEXS){
Map{{"<"}}Object,List{{"<"}}Conf{{.SheetName}}{{">"}}{{">"}} map = new HashMap{{"<"}}{{">"}}();
indexs.put(index, map);
}
}
/**
* 数据字段
* @author chuer
*/
public static final class K {
{{range .Props}}
/**{{.PNote}}*/
public static final String {{.PName}} = "{{.PName}}";
{{end}}
}
}
- 解析模版
package main
import (
"fmt"
"os"
"text/template"
)
type Prop struct {
PType string
PName string
PNote string
}
type Data struct {
PackageName string
Props []Prop
HaveDateProp bool
SuperClass string
ExcelName string
SheetName string
Indexs string
}
func main(){
p:= Data{"org.chuer.test",
[]Prop{{PType:"String",PName:"sn",PNote:"aa"},{PType:"Date",PName:"time",PNote:"bbb"}},
true,"Action","aaa.xlsx","Solider","time"}
filename := "C:\\Users\\Administrator\\Desktop\\conf.template"
t := template.New("conf.template")
t, _ = t.ParseFiles(filename)
err := t.Execute(os.Stdout, p)
if err != nil {
fmt.Println("bbb,",err)
}
}
- 解析结果
package org.chuer.test;
import java.util.Collections;
import java.util.Comparator;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import com.lehoo.sob.confsuper.Action;
/**
* excel|aaa.xlsx
* @author administrator
* 此类是系统自动生成类 不要直接修改,修改后也会被覆盖
*/
@JSONListener
public class ConfSolider extends Action{
/** 对应的数据文件 */
private static final String JSON_NAME = "ConfSolider.json";
/**索引*/
private static final String [] INDEXS = time;
/** aa */
private String sn;
/** bbb */
private Date time;
/** 配置数据 */
private static Map<Object,ConfSolider> datas = new LinkedHashMap<>();
/**索引结构,加快查询速度*/
private static Map<String,Map<Object,List<ConfSolider>>> indexs = new HashMap<>();
/** 私有构造函数 */
private ConfSolider(){ }
/**初始化索引*/
private static void initIndex(){
//初始化索引结构
for(String index : INDEXS){
Map<Object,List<ConfSolider>> map = new HashMap<>();
indexs.put(index, map);
}
}
/**
* 数据字段
* @author chuer
*/
public static final class K {
/**aa*/
public static final String sn = "sn";
/**bbb*/
public static final String time = "time";
}
}
Process finished with exit code 0