duanhuan5409 2019-04-02 01:39
浏览 93

我应该在每个http请求上调用template.ParseFiles(…)还是从主函数调用一次?

I am doing some web develoment using the go programming language using the package html/template. At some point of the code, I need to call the function template.ParseFiles(...) so I can create a template from those files ad then execute it using temp.Execute(w,data). I would like to know if it is better to create the template on each request or to do it once in the main and declare a global variable.

Right now I do it on each request on my handle functions, like most tutorials do. However, I don't know If I'm wasting resources by doing it on each request instead of having them as global variables.

This is how it looks on each request

func ViewStats(w http.ResponseWriter, r *http.Request) {
    //Get stuff from db and put them in data
    //...
    //return data to user
    tmp, err := template.ParseFiles("views/guest-layout.html",
        "views/stats.html")

    if err != nil {
        fmt.Println(err)
    } else {
        tmp.Execute(w,data)
    }
}

I would like to know if this is better:

var temp1 template.Template

func main() {
    temp1, err = template.ParseFiles("file1","file2") 
    //...
}
  • 写回答

1条回答 默认 最新

  • dpps0715 2019-04-02 06:31
    关注

    As usual: It depends.

    But first some nuance:

    1. You should never do template parsing (or anything else interesting) in your main() function. Instead, your main() function should call methods (or a single method) that kicks off the chain of interesting things in your program.

    2. Go doesn't have globals, so it's not actually an option to store your parsed templates in a global variable in the first place. The closest Go has to global variables is package variables. You could store your parsed templates in a package variable in the main package, but this is also bad form, as your main package (except for tiny, trivial programs), should just be an entry point, and otherwise nearly empty.

    But now, on to the core of your question:

    Should you parse templates per request, or per run?

    And here it depends.

    If you have templates that change frequently (i.e. during development, when you're constantly editing your HTML files), once per request can be best.

    But this is far less efficient than just parsing once, so in production, you may wish to parse the templates once on startup only. Then you can store the templates in a package variable, or better, in a struct that is initialized at runtime. But I leave that to you.

    But what may be best is actually a bit of a compromise between the two approaches. It may be best to load your templates at start-up, and re-load them occasionally, either automatically (say, every 5 minutes), or watch your filesystem, and reload them whenever the on-disk representation of the templates changes.

    How to do this is left as an exercise for the reader.

    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?