duanna2026 2013-07-28 17:22
浏览 67
已采纳

<!DOCTYPE> html /模板

I've got this simple go lang webserver that does nothing more but parsing some data into an external HTML file and serve that file to the webserver.

package main

import (
    "html/template"
    "net/http"
)

type Event struct {
    Name string
}

func handler(w http.ResponseWriter, r *http.Request) {
    e := Event{ Name: "Melt! Festival" }
    t, _ := template.ParseFiles("events.html")
    t.Execute(w, e)
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":1337", nil)
}

But whenever I try to parse the HTML file with the set it parses my html-page as text in stead of rendering the HTML in the browser

<!DOCTYPE>
<html>
<head>
    <title>Event</title>
</head>
<body>
    <p>
        Event: {{.Name}}
    </p>
</body>
</html>

When I leave the <!DOCTYPE> out of the HTML-file it renders it just fine.

Can anyone tell me why this is because I'm really curious? I spent two hours searching for the cause of my go code not working.

  • 写回答

2条回答 默认 最新

  • dongxuan1314 2013-07-28 17:26
    关注

    Your doctype declaration is incorrect, thus having an effect opposite from the desired one : it is probably interpreted by the browser as signifying the document isn't HTML.

    Use this :

    <!DOCTYPE html>
    

    See reference.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度