stringFromTemplate.go 454 B

1234567891011121314151617181920212223
  1. package stringfromtemplate
  2. import (
  3. "strings"
  4. "text/template"
  5. )
  6. //ToString returns string generated from given object using provided text template.
  7. func ToString(templateString string, object interface{}) string {
  8. var buffer strings.Builder
  9. tmpl, err := template.New("default").Parse(templateString)
  10. if err != nil {
  11. return err.Error()
  12. }
  13. err = tmpl.Execute(&buffer, object)
  14. if err != nil {
  15. return err.Error()
  16. }
  17. return buffer.String()
  18. }