{"id":210,"date":"2025-12-01T14:00:07","date_gmt":"2025-12-01T06:00:07","guid":{"rendered":"https:\/\/caoxunyi.cn\/?p=210"},"modified":"2025-12-01T14:00:07","modified_gmt":"2025-12-01T06:00:07","slug":"%e5%ae%9e%e6%88%98-%e7%94%a8-go-web-components-%e6%92%b8%e4%b8%80%e4%b8%aa%e8%bd%bb%e9%87%8f%e7%ba%a7%e8%af%84%e8%ae%ba%e7%b3%bb%e7%bb%9f","status":"publish","type":"post","link":"https:\/\/caoxunyi.cn\/index.php\/210\/","title":{"rendered":"[\u5b9e\u6218] \u7528 Go + Web Components \u64b8\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u8bc4\u8bba\u7cfb\u7edf"},"content":{"rendered":"<p>\u60f3\u8981\u7ed9\u7f51\u7ad9\u52a0\u4e2a\u8bc4\u8bba\u529f\u80fd\uff0c\u63a5\u5165\u7b2c\u4e09\u65b9\u670d\u52a1\u6015\u6570\u636e\u4e0d\u5b89\u5168\uff0c\u5f15\u5165\u5e9e\u5927\u7684\u524d\u7aef\u6846\u67b6\u53c8\u89c9\u5f97\u201c\u6740\u9e21\u7109\u7528\u725b\u5200\u201d\uff1f<\/p>\n<p>\u5176\u5b9e\uff0c\u5229\u7528\u6d4f\u89c8\u5668\u73b0\u4ee3\u6807\u51c6 <strong>Web Components<\/strong> \u548c\u7f16\u8bd1\u6781\u5176\u65b9\u4fbf\u7684 <strong>Go \u8bed\u8a00<\/strong>\uff0c\u6211\u4eec\u53ef\u4ee5\u5feb\u901f\u6784\u5efa\u4e00\u4e2a<strong>\u65e0\u4f9d\u8d56\u3001\u9ad8\u6027\u80fd<\/strong>\u7684\u5168\u6808\u8bc4\u8bba\u7cfb\u7edf\u3002\u5b83\u4e0d\u9700\u8981\u590d\u6742\u7684\u6784\u5efa\u6d41\u7a0b\uff0c\u90e8\u7f72\u65f6\u53ea\u9700\u4e00\u4e2a\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u975e\u5e38\u9002\u5408\u4e2a\u4eba\u5f00\u53d1\u8005\u5feb\u901f\u4e0a\u624b\u3002<\/p>\n<h2>\u6280\u672f\u6808\u4e00\u89c8<\/h2>\n<ul>\n<li><strong>Backend (\u540e\u7aef):<\/strong> Go (Gin \u6846\u67b6) + Gorm + SQLite\n<ul>\n<li><em>\u7406\u7531\uff1a\u90e8\u7f72\u6781\u5176\u7b80\u5355\uff0c\u7f16\u8bd1\u540e\u53ea\u662f\u4e00\u4e2a\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u81ea\u5e26\u6570\u636e\u5e93\uff0c\u65e0\u9700\u989d\u5916\u5b89\u88c5 MySQL\u3002<\/em><\/li>\n<\/ul><\/li>\n<li><strong>Frontend (\u524d\u7aef):<\/strong> \u539f\u751f JavaScript + Web Components\n<ul>\n<li><em>\u7406\u7531\uff1a\u65e0\u4f9d\u8d56\uff0c\u65e0\u6784\u5efa\u6d41\u7a0b\uff0c\u6807\u51c6 HTML \u6807\u7b7e\uff0c\u771f\u6b63\u7684\u7ec4\u4ef6\u5316\u3002<\/em><\/li>\n<\/ul><\/li>\n<\/ul>\n<hr \/>\n<h2>\u7b2c\u4e00\u90e8\u5206\uff1a\u540e\u7aef \u2014\u2014 \u6781\u901f\u642d\u5efa API<\/h2>\n<p>\u6211\u4eec\u5c06\u4f7f\u7528 Go \u8bed\u8a00\u6700\u6d41\u884c\u7684 Gin \u6846\u67b6\uff0c\u914d\u5408 Gorm \u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u3002\u6574\u4e2a\u540e\u7aef\u4ee3\u7801\u4e0d\u5230 60 \u884c\u3002<\/p>\n<h3>1. \u521d\u59cb\u5316\u9879\u76ee<\/h3>\n<p>\u9996\u5148\uff0c\u5728\u7ec8\u7aef\u4e2d\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u5939\u5e76\u521d\u59cb\u5316 Go \u6a21\u5757\uff1a<\/p>\n<pre><code class=\"lang-bash language-bash bash\">mkdir go-comments\ncd go-comments\ngo mod init go-comments<\/code><\/pre>\n<p>\u5b89\u88c5\u5fc5\u8981\u7684\u4f9d\u8d56\u5305\uff1a<\/p>\n<pre><code class=\"lang-bash language-bash bash\"># Web \u6846\u67b6\ngo get -u github.com\/gin-gonic\/gin\n# ORM \u548c \u6570\u636e\u5e93\u9a71\u52a8\ngo get -u gorm.io\/gorm\ngo get -u gorm.io\/driver\/sqlite\n# \u8de8\u57df\u5904\u7406\u4e2d\u95f4\u4ef6\ngo get -u github.com\/gin-contrib\/cors<\/code><\/pre>\n<h3>2. \u7f16\u5199 <code>main.go<\/code><\/h3>\n<p>\u65b0\u5efa <code>main.go<\/code>\uff0c\u8fd9\u662f\u6211\u4eec\u8981\u5b9e\u73b0\u7684\u5168\u90e8\u903b\u8f91\uff1a<\/p>\n<pre><code class=\"lang-go language-go go\">package main\n\nimport (\n    &quot;time&quot;\n\n    &quot;github.com\/gin-contrib\/cors&quot;\n    &quot;github.com\/gin-gonic\/gin&quot;\n    &quot;gorm.io\/driver\/sqlite&quot;\n    &quot;gorm.io\/gorm&quot;\n)\n\n\/\/ \u5b9a\u4e49\u8bc4\u8bba\u6a21\u578b\uff0cGorm \u4f1a\u81ea\u52a8\u628a\u5b83\u8f6c\u6362\u6210\u6570\u636e\u5e93\u8868\ntype Comment struct {\n    ID        uint      `gorm:&quot;primaryKey&quot; json:&quot;id&quot;`\n    Content   string    `json:&quot;content&quot;`\n    CreatedAt time.Time `json:&quot;created_at&quot;`\n}\n\nfunc main() {\n    \/\/ 1. \u8fde\u63a5 SQLite \u6570\u636e\u5e93 (\u81ea\u52a8\u521b\u5efa comments.db)\n    db, err := gorm.Open(sqlite.Open(&quot;comments.db&quot;), &amp;gorm.Config{})\n    if err != nil {\n        panic(&quot;\u6570\u636e\u5e93\u8fde\u63a5\u5931\u8d25&quot;)\n    }\n\n    \/\/ 2. \u81ea\u52a8\u8fc1\u79fb\uff1a\u4fdd\u8bc1\u6570\u636e\u5e93\u8868\u7ed3\u6784\u4e0e Struct \u540c\u6b65\n    db.AutoMigrate(&amp;Comment{})\n\n    \/\/ 3. \u521d\u59cb\u5316 Gin \u5f15\u64ce\n    r := gin.Default()\n\n    \/\/ 4. \u914d\u7f6e CORS (\u5141\u8bb8\u8de8\u57df)\n    \/\/ \u56e0\u4e3a\u524d\u7aef\u53ef\u80fd\u8dd1\u5728\u4e0d\u540c\u7aef\u53e3\uff0c\u5fc5\u987b\u5141\u8bb8\u8de8\u57df\u8bf7\u6c42\n    r.Use(cors.Default())\n\n    \/\/ 5. \u8def\u7531\u5b9a\u4e49\n\n    \/\/ \u83b7\u53d6\u8bc4\u8bba\u5217\u8868\n    r.GET(&quot;\/comments&quot;, func(c *gin.Context) {\n        var comments []Comment\n        \/\/ \u6309\u65f6\u95f4\u5012\u5e8f\u67e5\u8be2\n        db.Order(&quot;created_at desc&quot;).Find(&amp;comments)\n        c.JSON(200, comments)\n    })\n\n    \/\/ \u63d0\u4ea4\u65b0\u8bc4\u8bba\n    r.POST(&quot;\/comments&quot;, func(c *gin.Context) {\n        var jsonInput Comment\n        \/\/ \u7ed1\u5b9a JSON \u6570\u636e\n        if err := c.ShouldBindJSON(&amp;jsonInput); err != nil {\n            c.JSON(400, gin.H{&quot;error&quot;: err.Error()})\n            return\n        }\n        \/\/ \u5199\u5165\u6570\u636e\u5e93\n        db.Create(&amp;jsonInput)\n        c.JSON(200, jsonInput)\n    })\n\n    \/\/ 6. \u5728 8080 \u7aef\u53e3\u542f\u52a8\u670d\u52a1\n    r.Run(&quot;:8080&quot;)\n}<\/code><\/pre>\n<p>\u8fd0\u884c\u540e\u7aef\u975e\u5e38\u7b80\u5355\uff1a<\/p>\n<pre><code class=\"lang-bash language-bash bash\">go run main.go<\/code><\/pre>\n<p>\u770b\u5230 <code>Listening and serving HTTP on :8080<\/code> \u5373\u8868\u793a\u6210\u529f\u3002<\/p>\n<hr \/>\n<h2>\u7b2c\u4e8c\u90e8\u5206\uff1a\u524d\u7aef \u2014\u2014 \u5c01\u88c5 Web Components<\/h2>\n<p>\u6211\u4eec\u8981\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6807\u7b7e <code>&lt;my-comments&gt;<\/code>\uff0c\u65e0\u8bba\u628a\u5b83\u653e\u5728\u54ea\u4e2a HTML \u9875\u9762\u91cc\uff0c\u5b83\u90fd\u80fd\u81ea\u52a8\u6e32\u67d3\u51fa\u8bc4\u8bba\u533a\u5e76\u62e5\u6709\u4ea4\u4e92\u529f\u80fd\u3002<\/p>\n<h3>\u7f16\u5199 <code>index.html<\/code><\/h3>\n<p>\u4e3a\u4e86\u6f14\u793a\u65b9\u4fbf\uff0c\u6211\u4eec\u5c06 JS \u76f4\u63a5\u5199\u5728 HTML \u91cc\uff08\u5b9e\u9645\u5f00\u53d1\u4e2d\u5efa\u8bae\u5206\u79bb\u4e3a <code>.js<\/code> \u6587\u4ef6\uff09\u3002<\/p>\n<pre><code class=\"lang-html language-html html\">&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;zh-CN&quot;&gt;\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n\n&lt;title&gt;Go + Web Components Demo&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n\n    &lt;my-comments&gt;&lt;\/my-comments&gt;\n\n&lt;script&gt;\n        \/\/ \u81ea\u5b9a\u4e49\u7ec4\u4ef6\u7c7b\n        class MyComments extends HTMLElement {\n            constructor() {\n                super();\n                \/\/ \u5f00\u542f Shadow DOM\uff0c\u8ba9\u6837\u5f0f\u4e0e\u5916\u90e8\u9694\u79bb\uff0c\u4e92\u4e0d\u5f71\u54cd\n                this.attachShadow({ mode: 'open' });\n                this.apiUrl = 'http:\/\/localhost:8080\/comments'; \n            }\n\n            connectedCallback() {\n                this.render();\n                this.fetchComments(); \/\/ \u7ec4\u4ef6\u52a0\u8f7d\u65f6\u62c9\u53d6\u6570\u636e\n            }\n\n            render() {\n                this.shadowRoot.innerHTML = `\n\n&lt;style&gt;\n                    :host { display: block; font-family: sans-serif; }\n                    .container { \n                        max-width: 500px; margin: 20px auto; \n                        border: 1px solid #e0e0e0; border-radius: 8px; padding: 20px;\n                        box-shadow: 0 2px 10px rgba(0,0,0,0.05);\n                    }\n                    h3 { margin-top: 0; color: #333; }\n                    ul { list-style: none; padding: 0; max-height: 300px; overflow-y: auto; }\n                    li { border-bottom: 1px solid #f0f0f0; padding: 10px 0; }\n                    .content { font-size: 16px; color: #444; }\n                    .time { font-size: 12px; color: #999; margin-top: 4px; }\n                    .input-group { display: flex; gap: 10px; margin-top: 20px; }\n                    input { flex: 1; padding: 8px; border: 1px solid #ccc; border-radius: 4px; }\n                    button { \n                        padding: 8px 16px; background: #007bff; color: white; \n                        border: none; border-radius: 4px; cursor: pointer; \n                    }\n                    button:hover { background: #0056b3; }\n                &lt;\/style&gt;\n\n                &lt;div class=&quot;container&quot;&gt;\n\n&lt;h3&gt;\ud83d\udcac \u8bc4\u8bba\u533a&lt;\/h3&gt;\n                    &lt;ul id=&quot;list&quot;&gt;\u52a0\u8f7d\u4e2d...&lt;\/ul&gt;\n                    &lt;div class=&quot;input-group&quot;&gt;\n                        &lt;input type=&quot;text&quot; id=&quot;input&quot; placeholder=&quot;\u5199\u4e0b\u4f60\u7684\u60f3\u6cd5...&quot; \/&gt;\n                        &lt;button id=&quot;btn&quot;&gt;\u53d1\u9001&lt;\/button&gt;\n                    &lt;\/div&gt;\n                &lt;\/div&gt;\n                `;\n\n                \/\/ \u7ed1\u5b9a\u70b9\u51fb\u4e8b\u4ef6\n                this.shadowRoot.getElementById('btn').onclick = () =&gt; this.postComment();\n            }\n\n            \/\/ \u83b7\u53d6\u8bc4\u8bba (GET)\n            async fetchComments() {\n                try {\n                    const res = await fetch(this.apiUrl);\n                    const data = await res.json();\n\n                    const list = this.shadowRoot.getElementById('list');\n                    if (data.length === 0) {\n                        list.innerHTML = '&lt;li style=&quot;text-align:center;color:#999&quot;&gt;\u6682\u65e0\u8bc4\u8bba\uff0c\u5feb\u6765\u62a2\u6c99\u53d1\uff01&lt;\/li&gt;';\n                        return;\n                    }\n\n                    list.innerHTML = data.map(c =&gt; `\n\n&lt;li&gt;\n                            &lt;div class=&quot;content&quot;&gt;${this.escapeHtml(c.content)}&lt;\/div&gt;\n                            &lt;div class=&quot;time&quot;&gt;${new Date(c.created_at).toLocaleString()}&lt;\/div&gt;\n                        &lt;\/li&gt;\n                    `).join('');\n                } catch (err) {\n                    console.error(&quot;API Error:&quot;, err);\n                    this.shadowRoot.getElementById('list').innerHTML = '\u52a0\u8f7d\u5931\u8d25\uff0c\u8bf7\u68c0\u67e5\u540e\u7aef\u670d\u52a1\u3002';\n                }\n            }\n\n            \/\/ \u63d0\u4ea4\u8bc4\u8bba (POST)\n            async postComment() {\n                const input = this.shadowRoot.getElementById('input');\n                const content = input.value.trim();\n                if (!content) return;\n\n                try {\n                    await fetch(this.apiUrl, {\n                        method: 'POST',\n                        headers: { 'Content-Type': 'application\/json' },\n                        body: JSON.stringify({ content: content })\n                    });\n\n                    input.value = ''; \/\/ \u6e05\u7a7a\u8f93\u5165\u6846\n                    this.fetchComments(); \/\/ \u5237\u65b0\u5217\u8868\n                } catch (err) {\n                    alert(&quot;\u53d1\u9001\u5931\u8d25\uff01&quot;);\n                }\n            }\n\n            \/\/ \u7b80\u5355\u7684 XSS \u9632\u5fa1\n            escapeHtml(text) {\n                return text\n                    .replace(\/&amp;\/g, &quot;&amp;amp;&quot;)\n                    .replace(\/&lt;\/g, &quot;&amp;lt;&quot;)\n                    .replace(\/&gt;\/g, &quot;&amp;gt;&quot;)\n                    .replace(\/&quot;\/g, &quot;&amp;quot;&quot;)\n                    .replace(\/'\/g, &quot;&amp;#039;&quot;);\n            }\n        }\n\n        \/\/ \u6ce8\u518c\u7ec4\u4ef6\n        customElements.define('my-comments', MyComments);\n    &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<hr \/>\n<h2>\u8fd0\u884c\u6548\u679c<\/h2>\n<ol>\n<li>\n<p>\u4fdd\u6301 <code>go run main.go<\/code> \u5728\u7ec8\u7aef\u8fd0\u884c\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/gastigado.cnies.org\/d\/public\/e484d4c0515f42fc37b7567981c4c69f.png\" alt=\"\" \/><\/p>\n<\/li>\n<li>\n<p>\u53cc\u51fb\u6253\u5f00 <code>index.html<\/code>\u3002<\/p>\n<\/li>\n<li>\n<p>\u8f93\u5165\u8bc4\u8bba\u5e76\u53d1\u9001\uff0c\u4f60\u4f1a\u53d1\u73b0\u8bc4\u8bba\u7acb\u523b\u4fdd\u5b58\u5230\u4e86 SQLite \u6570\u636e\u5e93\u4e2d\uff0c\u5237\u65b0\u9875\u9762\u6570\u636e\u4f9d\u7136\u5b58\u5728\u3002<\/p>\n<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/gastigado.cnies.org\/d\/public\/299e4886297f80aa2049beaa2035881e.png\" alt=\"\" \/><\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u901a\u8fc7\u8fd9\u4e2a\u5c0f Demo\uff0c\u6211\u4eec\u53d1\u73b0\uff1a<\/p>\n<ol>\n<li><strong>Go \u505a\u540e\u7aef\u771f\u7684\u5f88\u723d<\/strong>\uff1a\u4e0d\u7528\u914d\u7f6e\u590d\u6742\u7684 Webpack\/Vite\uff0c\u4e0d\u7528\u62c5\u5fc3\u6570\u636e\u5e93\u5b89\u88c5\uff0c\u4e00\u4e2a\u4e8c\u8fdb\u5236\u6587\u4ef6\u8d70\u5929\u4e0b\u3002<\/li>\n<li><strong>Web Components \u5f88\u5f3a\u5927<\/strong>\uff1a\u5b83\u8ba9\u6211\u4eec\u56de\u5f52 HTML \u7684\u672c\u8d28\u3002\u60f3\u8c61\u4e00\u4e0b\uff0c\u4f60\u53ef\u4ee5\u5728\u4f60\u7684\u4e2a\u4eba\u535a\u5ba2\u3001\u516c\u53f8\u7684\u8001\u65e7\u9879\u76ee\uff0c\u751a\u81f3\u662f\u522b\u4eba\u7684\u7f51\u7ad9\uff08\u901a\u8fc7\u6cb9\u7334\u811a\u672c\uff09\u91cc\uff0c\u53ea\u7528\u4e00\u884c <code>&lt;my-comments&gt;&lt;\/my-comments&gt;<\/code> \u5c31\u63d2\u5165\u8fd9\u4e2a\u529f\u80fd\uff0c\u662f\u4e0d\u662f\u5f88\u9177\uff1f<\/li>\n<\/ol>\n<p>\u5e0c\u671b\u8fd9\u7bc7\u6559\u7a0b\u80fd\u7ed9\u4f60\u5e26\u6765\u7075\u611f\uff01Happy Coding! \u2728<\/p>","protected":false},"excerpt":{"rendered":"<p>\u60f3\u8981\u7ed9\u7f51\u7ad9\u52a0\u4e2a\u8bc4\u8bba\u529f\u80fd\uff0c\u63a5\u5165\u7b2c\u4e09\u65b9\u670d\u52a1\u6015\u6570\u636e\u4e0d\u5b89\u5168\uff0c\u5f15\u5165\u5e9e\u5927\u7684\u524d\u7aef\u6846\u67b6\u53c8\u89c9\u5f97\u201c\u6740\u9e21\u7109\u7528\u725b\u5200\u201d\uff1f \u5176\u5b9e\uff0c\u5229\u7528\u6d4f\u89c8\u5668\u73b0\u4ee3\u6807\u51c6 Web  &#8230;<\/p>","protected":false},"author":1,"featured_media":172,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[23],"tags":[],"class_list":["post-210","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web"],"_links":{"self":[{"href":"https:\/\/caoxunyi.cn\/index.php\/wp-json\/wp\/v2\/posts\/210","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/caoxunyi.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/caoxunyi.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/caoxunyi.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/caoxunyi.cn\/index.php\/wp-json\/wp\/v2\/comments?post=210"}],"version-history":[{"count":1,"href":"https:\/\/caoxunyi.cn\/index.php\/wp-json\/wp\/v2\/posts\/210\/revisions"}],"predecessor-version":[{"id":211,"href":"https:\/\/caoxunyi.cn\/index.php\/wp-json\/wp\/v2\/posts\/210\/revisions\/211"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/caoxunyi.cn\/index.php\/wp-json\/wp\/v2\/media\/172"}],"wp:attachment":[{"href":"https:\/\/caoxunyi.cn\/index.php\/wp-json\/wp\/v2\/media?parent=210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/caoxunyi.cn\/index.php\/wp-json\/wp\/v2\/categories?post=210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/caoxunyi.cn\/index.php\/wp-json\/wp\/v2\/tags?post=210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}