我正在通读 an article on golang templates这出现在示例代码中。
func renderTemplate(w http.ResponseWriter, name string, data map[string]interface{}) error {
// Ensure the template exists in the map.
tmpl, ok := templates[name]
if !ok {
return fmt.Errorf("The template %s does not exist.", name)
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
tmpl.ExecuteTemplate(w, "base", data)
return nil
}
我不明白的部分是函数声明中的参数data
:
data map[string]interface{}
我对go中的接口(interface)只有很基础的了解,但我不知道为什么会这样使用。
最佳答案
interface{}
就像 C 中的 (void*)
或 Java 中的 Object
,它意味着任何类型的值都可以是存储在 map 中。
关于go - 一个空的接口(interface)映射在 golang 函数中做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30793673/