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

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

如何獲得模板渲染的結(jié)果

如何獲得模板渲染的結(jié)果

Go
森林海 2021-05-03 17:26:22
我剛接觸golang。這是我的問(wèn)題:我想獲取template.Execute的字符串結(jié)果,并且我不想直接對(duì)http.ResponsWriter執(zhí)行這是我的代碼,它似乎無(wú)法正常工作package mainimport (    "fmt"    "os"    "template")type ByteSlice []bytefunc (p *ByteSlice) Write(data []byte) (lenght int, err os.Error) {    *p = data    return len(data), nil}func main() {    page := map[string]string{"Title": "Test Text"}    tpl, _ := template.ParseFile("test.html")    var b ByteSlice    tpl.Execute(&b, &page)    fmt.Printf(`"html":%s`, b)}和text.html:<html><body>    <h1>{{.Title|html}}</h1></body></html>但是我得到的是"html":</h1></body></html>
查看完整描述

1 回答

?
暮色呼如

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

ByteSlice的Write方法有問(wèn)題。它應(yīng)該將新數(shù)據(jù)附加到已寫(xiě)入的數(shù)據(jù)上,但是您的版本將替換已寫(xiě)入的數(shù)據(jù)。模板代碼可能多次調(diào)用Write,因此最終只能打印出最后寫(xiě)入的內(nèi)容。

代替創(chuàng)建ByteSlice,使用bytes.Buffer。


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

添加回答

舉報(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)