英文:
Golang - Where does the function "Execute" writes to the stream?
问题
我是Go的初学者,我不明白在Execute
函数的流中哪里调用了写入数据"home.html"的操作。http.ResponseWriter
是写入器,这一点很清楚,但是在Execute
函数中,我看不到类似于write .. fmt.Fprint..
的内容,我只看到了Execute
的递归调用。
http://golang.org/src/pkg/html/template/template.go?s=1245:1315#L40
//我的函数
func homeHandler(c http.ResponseWriter, req *http.Request) {
var homeTempl = template.Must(template.ParseFiles("home.html"))
//这里是我的疑惑
homeTempl.Execute(c, req.Host)
//这是一致的
fmt.Fprint(c, "hallo")
}
英文:
i´m beginner in Go and i dont understand where is the call to write the data "home.html" in the stream of the function Execute
.The http.ResponseWriter is the writter thats clear but in the function Execute
i cant see anything like write .. fmt.Fprint..
i only see the recursion of Execute
http://golang.org/src/pkg/html/template/template.go?s=1245:1315#L40
//my Function
func homeHandler(c http.ResponseWriter, req *http.Request) {
var homeTempl = template.Must(template.ParseFiles("home.html"))
//here is my misunderstanding
homeTempl.Execute(c, req.Host)
//Thats consistent
fmt.Fprint(c, "hallo")
}
答案1
得分: 1
这不是一个递归调用。它调用的是"text/template"包中的Template.Execute(而不是"html/template"中的Template.Execute)。这也是你可以找到实际写入字节到Writer的代码的地方。
http://golang.org/src/pkg/text/template/exec.go?s=2630:2700#L95
英文:
That's not a recursive call. It is calling Template.Execute in the "text/template" package (not Template.Execute in "html/template"). That is also where you will find the code that actually writes bytes on the Writer.
http://golang.org/src/pkg/text/template/exec.go?s=2630:2700#L95
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论