插入模板名称作为类

When creating a Go template, you can give it a name, like in this example, "my_home_template":

var tmplHome = template.Must(template.New("my_home_template").Funcs(funcMap).ParseFiles("templates/base.tmpl", "templates/content_home.tmpl"))

How can I get that template name and use it inside the actual template file?

Ultimately I just want to define a convenient css class, like so:

<body class="my_home_template">

Here's a working solution, taking mkopriva's advice:

When executing a template, pass some custom parameter with dummy data. Here, I just create a "PageHome" parameter to pass to the template, and value is a simple "1", but it could be any value:

tmplHome.ExecuteTemplate(w, "base", map[string]interface{}{"PageHome": "1", "Data": events, "UserFirstName": &u.FirstName, "UserProfilePic": &u.ProfilePic})

Then, inside the template itself, a simple if statement to check if the parameter exists, and do something accordingly:

{{ if .PageHome }}
    <body class="PageHome">
{{ else }}
    <body>
{{ end }}

All my other template executions don't pass a "PageHome" parameter at all, so the if statement never passes as true for them.

There's probably a more advanced solution using a functions via a template function map, and having a consistent "PageType":"something" parameter in all template executions, but in the end you still have to define a parameter per template execution and still have to build up if statements in your templates anyways.