Skip to content

Instantly share code, notes, and snippets.

@AICDEV
Created April 25, 2021 16:29
Show Gist options
  • Save AICDEV/f58acc0e5bafebc17097f72fbaf8211e to your computer and use it in GitHub Desktop.
Save AICDEV/f58acc0e5bafebc17097f72fbaf8211e to your computer and use it in GitHub Desktop.
Golang - Template execution to string
package core
import (
"fmt"
"strings"
"text/template"
)
type TplParser interface {
GetTemplate() string
}
type TplResult struct {
Out []string
}
func (r *TplResult) Write(p []byte) (n int, err error) {
r.Out = append(r.Out, string(p))
return len(p), nil
}
func (r *TplResult) GetTemplate() string {
return strings.Join(r.Out, "")
}
func Test() {
z, _ := template.New("foo").Parse(GenerateApplicationError())
f := &TplResult{}
z.Execute(f, z)
fmt.Println(f.GetTemplate())
}
func GenerateApplicationError() string {
return `
package utils
type ApplicationError struct {
Message string ` + "`json:\"message\"" + `
StatusCode int ` + "`json:\"status\"" + `
Code string ` + "`json:\"code\"" + `
}
`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment