Created
April 25, 2021 16:29
-
-
Save AICDEV/f58acc0e5bafebc17097f72fbaf8211e to your computer and use it in GitHub Desktop.
Golang - Template execution to string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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