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

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么 template.ParseFiles() 沒有檢測到這個錯誤?

為什么 template.ParseFiles() 沒有檢測到這個錯誤?

Go
收到一只叮咚 2022-11-28 17:04:23
如果我在我的模板文件中指定了一個不存在的模板,則錯誤不是由 ParseFiles() 而是由 ExecuteTemplate() 檢測到的。人們會期望解析來檢測任何丟失的模板。在解析期間檢測此類錯誤也可能導(dǎo)致性能改進(jìn)。{{define "test"}}<html>    <head>        <title> test </title>    </head>    <body>        <h1> Hello, world!</h1>        {{template "doesnotexist"}}    </body></html>{{end}}主程序package mainimport (    "html/template"    "os"    "fmt")func main() {    t, err := template.ParseFiles("test.html")    if err != nil {        fmt.Printf("ParseFiles: %s\n", err)        return    }    err = t.ExecuteTemplate(os.Stdout, "test", nil)    if err != nil {        fmt.Printf("ExecuteTemplate: %s\n", err)    }}10:46:30 $ go run main.go ExecuteTemplate: html/template:test.html:8:19: no such template "doesnotexist"10:46:31 $ 
查看完整描述

1 回答

?
元芳怎么了

TA貢獻(xiàn)1798條經(jīng)驗 獲得超7個贊

template.ParseFiles()不報告丟失的模板,因為通常不是所有的模板都在一個步驟中被解析,并且報告丟失的模板(by template.ParseFiles())不允許這樣做。

可以使用來自多個來源的多個調(diào)用來解析模板。

例如,如果您調(diào)用該Template.Parse()方法或您的模板,您可以向其添加更多模板:

_, err = t.Parse(`{{define "doesnotexist"}}the missing piece{{end}}`)

if err != nil {

    fmt.Printf("Parse failed: %v", err)

    return

}

上面的代碼將添加缺失的部分,您的模板執(zhí)行將成功并生成輸出(在Go Playground上嘗試):


<html>

    <head>

        <title> test </title>

    </head>

    <body>

        <h1> Hello, world!</h1>

        the missing piece

    </body>

</html>

更進(jìn)一步,不需要解析和“呈現(xiàn)”所有模板為您提供了優(yōu)化的可能性。可能存在“普通”用戶永遠(yuǎn)不會使用的管理頁面,并且僅當(dāng)管理員用戶啟動或使用您的應(yīng)用程序時才需要。在這種情況下,您可以通過不必解析管理頁面(僅當(dāng)/如果管理員用戶使用您的應(yīng)用程序)來加速啟動和相同的內(nèi)存。


查看完整回答
反對 回復(fù) 2022-11-28
  • 1 回答
  • 0 關(guān)注
  • 135 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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