第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

為什么當(dāng)我再次運(yùn)行 main.go 時(shí)視圖相同

為什么當(dāng)我再次運(yùn)行 main.go 時(shí)視圖相同

Go
湖上湖 2021-10-18 15:04:16
main.gopackage mainimport (    "html/template"    "net/http")var templates = template.Must(template.ParseGlob("./templates/*"))    func viewHandler(w http.ResponseWriter, r *http.Request) {    err := templates.ExecuteTemplate(w, "indexPage", nil)    if err != nil {        http.Error(w, err.Error(), http.StatusInternalServerError)        return    }}func main() {    http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))    http.HandleFunc("/index", viewHandler)    http.ListenAndServe(":8090", nil)}索引.html{{define "indexPage"}}<html>{{template "header"}}<body>    <nav class="navbar navbar-default">        <div class="container-fluid">            <div class="navbar-header">                <a class="navbar-brand" href="#">Welcome to TDT Project</a>            </div>        </div>    </nav>    <div class="btn-group-vertical">        <a href="#" class="btn btn-default">Button</a>        <a href="#" class="btn btn-default">Button</a>    </div></body></html>{{end}}另一個(gè) html 文件是 header.html 并且是正確的。修改html,再次運(yùn)行main.go,為什么視圖總是一樣?(我清理了瀏覽器的緩存)比如把“Welcome”改成“wwww”,瀏覽器確實(shí)變了。然后我殺掉main.go的進(jìn)度,再次運(yùn)行,視圖變了。有沒(méi)有更好的方法來(lái)停止 main.go 而不是殺死這個(gè)進(jìn)度?
查看完整描述

1 回答

?
波斯汪

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超4個(gè)贊

您只渲染一次模板作為templates全局變量。

每次可以將渲染移動(dòng)到函數(shù)時(shí)重新渲染模板。然而,這對(duì)性能不利,因?yàn)殇秩痉浅0嘿F,但在開發(fā)過(guò)程中沒(méi)問(wèn)題。例如,您可以使用@elithrar 提出的替代方案。另一種選擇是使用gin并修改它以掃描 html 文件,而不僅僅是 go 文件。


查看完整回答
反對(duì) 回復(fù) 2021-10-18
  • 1 回答
  • 0 關(guān)注
  • 200 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)