Skip to content

Instantly share code, notes, and snippets.

@zdunecki
Created October 24, 2022 10:40
Show Gist options
  • Save zdunecki/47dedf369c973e6f23e79624f47e3819 to your computer and use it in GitHub Desktop.
Save zdunecki/47dedf369c973e6f23e79624f47e3819 to your computer and use it in GitHub Desktop.
Check composition variables in go template engine
func TestTemplateCompositionStruct(t *testing.T) {
type Deep struct {
Name string
}
type M struct {
Deep
}
var b bytes.Buffer
te, _ := template.New("").Parse("You have new alert {{.Name}}")
data := &M{
Deep{Name: "Something went wrong"},
}
te.Execute(&b, &data)
current := b.String()
should := "You have new alert Something went wrong"
if current != should {
t.Fatal(fmt.Sprintf("'%s'", current), "is not equal", fmt.Sprintf("'%s'", should))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment