Golang – 函数”Execute”将写入到哪个流中?

huangapple go评论98阅读模式
英文:

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

huangapple
  • 本文由 发表于 2012年12月15日 08:39:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/13888204.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定