我最近开始学习Go.我有一个像网络应用程序的样本.我有:
/* tick-tock.go */
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
)
// Content for the main html page..
var page = `<html>
<head>
<script type="text/javascript"
src="http://localhost:8081/jquery.min.js">
</script>
<style>
div {
font-family: "Times New Roman", Georgia, Serif;
font-size: 1em;
width: 13.3em;
padding: 8px 8px;
border: 2px solid #2B1B17;
color: #2B1B17;
text-shadow: 1px 1px #E5E4E2;
background: #FFFFFF;
}
</style>
</head>
<body>
<h2 align=center>Go Timer </h2>
<div id="output" style="width: 30%; height: 63%; overflow-y: scroll; float:left;"></div>
<div id="v1" style="width: 50%; height: 30%; …Run Code Online (Sandbox Code Playgroud) 使用template包为客户端生成动态网页时速度太慢.
测试代码如下,golang 1.4.1
http.Handle("/js/", (http.FileServer(http.Dir(webpath))))
http.Handle("/css/", (http.FileServer(http.Dir(webpath))))
http.Handle("/img/", (http.FileServer(http.Dir(webpath))))
http.HandleFunc("/test", TestHandler)
func TestHandler(w http.ResponseWriter, r *http.Request) {
Log.Info("Entering TestHandler ...")
r.ParseForm()
filename := NiConfig.webpath + "/test.html"
t, err := template.ParseFiles(filename)
if err != nil {
Log.Error("template.ParseFiles err = %v", err)
}
t.Execute(w, nil)
}
Run Code Online (Sandbox Code Playgroud)
根据日志,我发现它花了大约3秒钟t.Execute(w, nil),我不知道它为什么用这么多时间.我也试过Apache服务器进行测试test.html,它响应速度非常快.