In the Go language, we can define functions in .go files and call them directly, or we can call functions in Go templates. However, there are some differences in how the function is run and called in both cases. When running a function in a .go file, we can call the function directly through the function name and parameter list. When calling the function in the template, we need to use {{}} syntax to wrap the function call and use the function name as the template directive. part. In addition, function calls in templates can be executed dynamically during template rendering, while when running functions in .go files, the execution of the functions is static and will not be affected by template rendering. Therefore, depending on the specific usage scenarios and needs, we can choose the appropriate way to run functions and call them.
Using template.funcmap
from the text/template
package, you can access functions directly from go template files.
Assume the following scenario: In the handler of the user overview page, call the function getallusers
and use executetemplate
to pass the user object to the template:
func index(w http.responsewriter, r *http.request) { users, err := model.getallusers() if err != nil { render50x() return } data := make(map[string]interface{}) data["userlist"] = users render(w, r, data, "layout", "index") }
Is this the same as passing a function to the template and executing it there?
var funcs = template.funcmap{ "getallusers": model.getallusers, } // func render t := template.new("render").funcs(funcs) if err := template.must(t.parsefs(viewsfs, files...)).executetemplate(w, layout, data); err != nil { log.println("error executing template:", err.error()) }
{{ range getAllUsers }} {{ .DisplayName }} {{ end }}
Is there any difference between these two methods?
It's the same thing if the function can be called from the template. Some differences:
If you call it in Go, you don't need to register the function. Sometimes you don't have access to template parsing to register a function, so this is the only way (don't forget: you have to register the function before parsing the template).
Additionally, if you call it in Go, you have more "control" over it: you can recover from panics, you can preprocess the results, and you can reuse it in other Go code. You can also choose not to execute the template based on the results, or perform other actions that may not be (easily) expressible in the template.
The result of a function may also not be easily rendered. For example. It may not be string
, or it may not have a String() string
method. Therefore, some additional (Go) logic may be required to convert the results into a human-readable format, which may not be available in the template, or more functions may need to be registered.
Also note that not all functions can be registered and called from templates. Callable functions can have up to 2 return types, and seconds can only be error
. From Go, you can call "any" function and pass only the results you need. If the function has parameters, you must also pass them as data to the template execution (so you can pass them into the template when calling the function).
The above is the detailed content of What is the difference between running a function in a .go file and calling it in a Go template?. For more information, please follow other related articles on the PHP Chinese website!