1. 程式人生 > >go 模版引擎簡單例項

go 模版引擎簡單例項

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