当我再次运行main.go时,为什么视图仍然相同?

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

Why views are same when I run main.go again

问题

main.go

package main

import (
	"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)
}

index.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}}

另外一个HTML文件是header.html,并且是正确的。

当我修改HTML并重新运行main.go时,为什么视图始终保持不变?(我已经清除了浏览器的缓存)例如,将"Welcome"更改为"wwww",浏览器根本没有变化。

然后我终止了main.go的进程并重新运行它,视图就改变了。

有没有比终止进程更好的方法来停止main.go?

英文:

main.go

package main
import (
	&quot;html/template&quot;
	&quot;net/http&quot;
)
var templates = template.Must(template.ParseGlob(&quot;./templates/*&quot;))

    func viewHandler(w http.ResponseWriter, r *http.Request) {
	err := templates.ExecuteTemplate(w, &quot;indexPage&quot;, nil)
    if err != nil {
    	http.Error(w, err.Error(), http.StatusInternalServerError)
    	return
    }
}

func main() {
    http.Handle(&quot;/static/&quot;, http.StripPrefix(&quot;/static/&quot;, http.FileServer(http.Dir(&quot;static&quot;))))
	http.HandleFunc(&quot;/index&quot;, viewHandler)
    http.ListenAndServe(&quot;:8090&quot;, nil)
}

index.html

{{define &quot;indexPage&quot;}}
&lt;html&gt;
{{template &quot;header&quot;}}
&lt;body&gt;
	&lt;nav class=&quot;navbar navbar-default&quot;&gt;
		&lt;div class=&quot;container-fluid&quot;&gt;
			&lt;div class=&quot;navbar-header&quot;&gt;
				&lt;a class=&quot;navbar-brand&quot; href=&quot;#&quot;&gt;Welcome to TDT Project&lt;/a&gt;
			&lt;/div&gt;
		&lt;/div&gt;
	&lt;/nav&gt;

	&lt;div class=&quot;btn-group-vertical&quot;&gt;
		&lt;a href=&quot;#&quot; class=&quot;btn btn-default&quot;&gt;Button&lt;/a&gt;
		&lt;a href=&quot;#&quot; class=&quot;btn btn-default&quot;&gt;Button&lt;/a&gt;
	&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
{{end}}

another html file is header.html and is correct.

When I change the html and run main.go again, why the views are always the same?(I have cleaned the cache of browser)For example, change "Welcome" to "wwww", the browser does change at all.

Then I kill the progress of main.go and run it again, the view is changed.

Is there a better way to stop the main.go rather than kill this progress?

答案1

得分: 0

你只需要将模板渲染的代码放在函数中,这样每次都会重新渲染模板。不过这样做会影响性能,因为渲染是比较耗费资源的,但在开发过程中可以接受。另一种选择是使用 @elithrar 提出的方法。另外,你还可以使用 gin,并修改它以扫描html文件,而不仅仅是go文件。

英文:

You are rendering your templates only once as templates is a global variable.

To re-render your templates each time you can move the rendering to your function. This however is bad for performance as rendering is quite expensive but it's ok during development. As an alternative you can for example use what @elithrar proposed. A further alternative would be to use gin and modify it to also scan for html files and not only go files.

huangapple
  • 本文由 发表于 2015年6月28日 20:56:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/31100051.html
匿名

发表评论

匿名网友

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

确定