为了账号安全,请及时绑定邮箱和手机立即绑定

没有第三方导入的 Golang 中的 html 渲染器

没有第三方导入的 Golang 中的 html 渲染器

Go
阿晨1998 2022-06-21 16:59:06
我正在自学 go 并且我已经开始尝试使用 go 作为后端。我有以下呈现 HTML 页面的代码,但我正在寻找一种不依赖第三方导入的解决方案,以便更好地掌握正在发生的事情和需要做的事情。package mainimport (    "log"    "net/http"    "github.com/thedevsaddam/renderer")var rnd *renderer.Renderfunc init() {    opts := renderer.Options{        ParseGlobPattern: "./pages/*.gohtml",    }    rnd = renderer.New(opts)}func main() {    mux := http.NewServeMux()    mux.HandleFunc("/", home)    mux.HandleFunc("/about", about)    port := ":9000"    log.Println("Listening on port ", port)    http.ListenAndServe(port, mux)}func home(w http.ResponseWriter, r *http.Request) {    rnd.HTML(w, http.StatusOK, "home", nil)}func about(w http.ResponseWriter, r *http.Request) {    rnd.HTML(w, http.StatusOK, "about", nil)}GOHTML 文件是:{{ define "home"}}<!DOCTYPE html><html><head>  <title>Home</title></head><body>  <h1>HOME</h1>  <ul>    <li><a href="/">Home</a></li>    <li><a href="/about">About</a></li>  </ul></body></html>{{ end }}{{ define "about" }}<!DOCTYPE html><html><head>  <title>About</title></head><body>  <h1>ABOUT</h1>  <ul>    <li><a href="/">Home</a></li>    <li><a href="/about">About</a></li>  </ul></body></html>{{ end }}任何帮助是极大的赞赏
查看完整描述

1 回答

?
慕妹3146593

TA贡献1820条经验 获得超9个赞

使用标准库中的html/template包。您使用的渲染器只是围绕它的一个薄包装器。



查看完整回答
反对 回复 2022-06-21
  • 1 回答
  • 0 关注
  • 123 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号