34 lines
551 B
Go
34 lines
551 B
Go
package util
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"text/template"
|
|
)
|
|
|
|
func TemplateToFile(desFile string, templateContent string, data interface{}) error {
|
|
file, err := os.OpenFile(desFile, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.ModePerm)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer file.Close()
|
|
|
|
tpl := template.New("template")
|
|
tpl, err = tpl.Parse(templateContent)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
b := bytes.Buffer{}
|
|
err = tpl.Execute(&b, data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = file.Write(b.Bytes())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|