/templatessample.tmplsample.yml
sample.tmpl
url : {{ .host }}
namespace: {{ .namespace }}

我在使用以下函数。

func ApplyTemplate(filePath string) (err error) {
    // Variables - host, namespace
    type Eingest struct {
        host      string
        namespace string
    }

    ei := Eingest{host: "example.com", namespace: "finance"}
    var templates *template.Template
    var allFiles []string
    files, err := ioutil.ReadDir(filePath)
    if err != nil {
        fmt.Println(err)
    }

    for _, file := range files {
        filename := file.Name()
        fullPath := filePath + "/" + filename
        if strings.HasSuffix(filename, ".tmpl") {
            allFiles = append(allFiles, fullPath)
        }
    }

    fmt.Println("Files in path: ", allFiles)

    // parses all .tmpl files in the 'templates' folder
    templates, err = template.ParseFiles(allFiles...)
    if err != nil {
        fmt.Println(err)
    }

    s1 := templates.Lookup("sample.tmpl")
    s1.ExecuteTemplate(os.Stdout, "sample.yml", ei)
    fmt.Println()
    return
}
s1.ExecuteTemplate()stdout