{"id":207045,"date":"2025-07-08T10:45:27","date_gmt":"2025-07-08T02:45:27","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207045\/"},"modified":"2025-07-08T10:45:27","modified_gmt":"2025-07-08T02:45:27","slug":"golang-gin%e5%8a%a0%e8%bd%bdhtml%e8%b6%85%e6%97%b6","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207045\/","title":{"rendered":"golang gin\u52a0\u8f7dhtml\u8d85\u65f6"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>golang gin\u52a0\u8f7dhtml\u8d85\u65f6<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-19 17:45:21<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300agolang gin\u52a0\u8f7dhtml\u8d85\u65f6\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>gin\u52a0\u8f7dindex.html\u65f6\u603b\u662f\u62a5\u9519<\/p>\n<pre>r := gin.new()\nr.use(gin.logger())\nr.use(gin.recovery())\nr.loadhtmlglob(\"views\/**\/*\")\nr.get(\"\/\", func(g *gin.context) {\n    g.html(http.statusok, \"index\/index.html\", gin.h{\n        \"title\":\"test\",\n    })<\/pre>\n<pre>2018\/11\/27 20:23:46 [Recovery] 2018\/11\/27 - 20:23:46 panic recovered:\nGET \/ HTTP\/1.1\nHost: localhost:8080\nAccept: text\/html,application\/xhtml+xml,application\/xml;q=0.9,image\/webp,image\/apng,*\/*;q=0.8\nAccept-Encoding: gzip, deflate, br\nAccept-Language: zh-CN,zh;q=0.9,en;q=0.8\nCache-Control: max-age=0\nConnection: keep-alive\nCookie: pix_k=admin; pix_v=L5lsdrVKJmpAa21DGibdbA%3D%3D\nUpgrade-Insecure-Requests: 1\nUser-Agent: Mozilla\/5.0 (Windows NT 6.1; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/70.0.3538.110 Safari\/537.36\n\n\nwrite tcp [::1]:8080-&gt;[::1]:55738: i\/o timeout\nC:\/Go\/src\/runtime\/panic.go:505 (0x4300c7)\n    gopanic: reflectcall(nil, unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz), uint32(d.siz))\nG:\/gowork\/src\/github.com\/gin-gonic\/gin\/context.go:725 (0xae81de)\n    (*Context).Render: panic(err)\nG:\/gowork\/src\/github.com\/gin-gonic\/gin\/context.go:734 (0xae82a6)\n    (*Context).HTML: c.Render(code, instance)\nG:\/gowork\/src\/ihaveadream\/routers\/routers.go:18 (0xaffa91)\n    InitRouter.func1: g.HTML(http.StatusOK, \"index\/index.html\", gin.H{\nG:\/gowork\/src\/github.com\/gin-gonic\/gin\/context.go:108 (0xae3952)\n    (*Context).Next: c.handlers[c.index](c)\nG:\/gowork\/src\/github.com\/gin-gonic\/gin\/recovery.go:52 (0xafca97)\n    RecoveryWithWriter.func1: c.Next()\nG:\/gowork\/src\/github.com\/gin-gonic\/gin\/context.go:108 (0xae3952)\n    (*Context).Next: c.handlers[c.index](c)\nG:\/gowork\/src\/github.com\/gin-gonic\/gin\/logger.go:84 (0xafb889)\n    LoggerWithWriter.func1: c.Next()\nG:\/gowork\/src\/github.com\/gin-gonic\/gin\/context.go:108 (0xae3952)\n    (*Context).Next: c.handlers[c.index](c)\nG:\/gowork\/src\/github.com\/gin-gonic\/gin\/gin.go:362 (0xaee92c)\n    (*Engine).handleHTTPRequest: c.Next()\nG:\/gowork\/src\/github.com\/gin-gonic\/gin\/gin.go:328 (0xaee403)\n    (*Engine).ServeHTTP: engine.handleHTTPRequest(c)\nC:\/Go\/src\/net\/http\/server.go:2694 (0x7643ca)\n    serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)\nC:\/Go\/src\/net\/http\/server.go:1830 (0x75f118)\n    (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)\nC:\/Go\/src\/runtime\/asm_amd64.s:2361 (0x45bcb0)\n    goexit: BYTE    $0x90   \/\/ NOP\n\n\n2018\/11\/27 20:23:46 [Recovery] 2018\/11\/27 - 20:23:46 panic recovered:\nGET \/ HTTP\/1.1\nHost: localhost:8080\nAccept: text\/html,application\/xhtml+xml,application\/xml;q=0.9,image\/webp,image\/apng,*\/*;q=0.8\nAccept-Encoding: gzip, deflate, br\nAccept-Language: zh-CN,zh;q=0.9,en;q=0.8\nCache-Control: max-age=0\nConnection: keep-alive\nCookie: pix_k=admin; pix_v=L5lsdrVKJmpAa21DGibdbA%3D%3D\nUpgrade-Insecure-Requests: 1\nUser-Agent: Mozilla\/5.0 (Windows NT 6.1; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/70.0.3538.110 Safari\/537.36\n\n\nwrite tcp [::1]:8080-&gt;[::1]:55736: i\/o timeout\nC:\/Go\/src\/runtime\/panic.go:505 (0x4300c7)\n    gopanic: reflectcall(nil, unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz), uint32(d.siz))\nG:\/gowork\/src\/github.com\/gin-gonic\/gin\/context.go:725 (0xae81de)\n    (*Context).Render: panic(err)\nG:\/gowork\/src\/github.com\/gin-gonic\/gin\/context.go:734 (0xae82a6)\n    (*Context).HTML: c.Render(code, instance)\nG:\/gowork\/src\/ihaveadream\/routers\/routers.go:18 (0xaffa91)\n    InitRouter.func1: g.HTML(http.StatusOK, \"index\/index.html\", gin.H{\nG:\/gowork\/src\/github.com\/gin-gonic\/gin\/context.go:108 (0xae3952)\n    (*Context).Next: c.handlers[c.index](c)\nG:\/gowork\/src\/github.com\/gin-gonic\/gin\/recovery.go:52 (0xafca97)\n    RecoveryWithWriter.func1: c.Next()\nG:\/gowork\/src\/github.com\/gin-gonic\/gin\/context.go:108 (0xae3952)\n    (*Context).Next: c.handlers[c.index](c)\nG:\/gowork\/src\/github.com\/gin-gonic\/gin\/logger.go:84 (0xafb889)\n    LoggerWithWriter.func1: c.Next()\nG:\/gowork\/src\/github.com\/gin-gonic\/gin\/context.go:108 (0xae3952)\n    (*Context).Next: c.handlers[c.index](c)\nG:\/gowork\/src\/github.com\/gin-gonic\/gin\/gin.go:362 (0xaee92c)\n    (*Engine).handleHTTPRequest: c.Next()\nG:\/gowork\/src\/github.com\/gin-gonic\/gin\/gin.go:328 (0xaee403)\n    (*Engine).ServeHTTP: engine.handleHTTPRequest(c)\nC:\/Go\/src\/net\/http\/server.go:2694 (0x7643ca)\n    serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)\nC:\/Go\/src\/net\/http\/server.go:1830 (0x75f118)\n    (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)\nC:\/Go\/src\/runtime\/asm_amd64.s:2361 (0x45bcb0)\n    goexit: BYTE    $0x90   \/\/ NOP<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u5982\u679c\u60a8\u7684index.html \u6587\u4ef6\u4f4d\u4e8eviews\/index\/index.html \u4e2d\uff0c\u5219\u5e94\u4f7f\u7528\u6b64\u901a\u914d\u7b26\u3002\u4e5f\u5c1d\u8bd5\u4f7f\u7528 <code>.\/<\/code>\u3002<\/p>\n<pre>r.LoadHTMLGlob(\".\/views\/*\")\nr.GET(\"\/\", func(g *gin.Context) {\n  g.HTML(http.StatusOK, \"index\/index.html\", gin.H{\n    \"title\":\"test\",\n  }\n)<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300agolang gin\u52a0\u8f7dhtml\u8d85\u65f6\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207045","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207045","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207045"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207045\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207045"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207045"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}