{"id":1445,"date":"2025-02-08T23:52:43","date_gmt":"2025-02-08T15:52:43","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1445"},"modified":"2025-02-08T23:52:44","modified_gmt":"2025-02-08T15:52:44","slug":"go%e8%af%ad%e8%a8%80%e9%a1%b9%e7%9b%ae%e5%bc%80%e5%8f%91%e4%b8%8e%e5%b7%a5%e5%85%b7%e9%93%be%ef%bc%9a%e5%a6%82%e4%bd%95%e6%9e%84%e5%bb%ba%e5%92%8c%e9%83%a8%e7%bd%b2go%e5%ba%94%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/08\/go%e8%af%ad%e8%a8%80%e9%a1%b9%e7%9b%ae%e5%bc%80%e5%8f%91%e4%b8%8e%e5%b7%a5%e5%85%b7%e9%93%be%ef%bc%9a%e5%a6%82%e4%bd%95%e6%9e%84%e5%bb%ba%e5%92%8c%e9%83%a8%e7%bd%b2go%e5%ba%94%e7%94%a8\/","title":{"rendered":"Go\u8bed\u8a00\u9879\u76ee\u5f00\u53d1\u4e0e\u5de5\u5177\u94fe\uff1a\u5982\u4f55\u6784\u5efa\u548c\u90e8\u7f72Go\u5e94\u7528"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728Go\u8bed\u8a00\u7684\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u9879\u76ee\u7684\u7ed3\u6784\u548c\u7ec4\u7ec7\u4ee5\u53ca\u5982\u4f55\u9ad8\u6548\u5730\u90e8\u7f72\u548c\u53d1\u5e03\u5e94\u7528\u7a0b\u5e8f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u901a\u8fc7\u4e86\u89e3Go\u8bed\u8a00\u9879\u76ee\u7ed3\u6784\u3001\u4f7f\u7528Go\u521b\u5efaWeb\u670d\u52a1\u3001\u90e8\u7f72Go\u5e94\u7528\u4ee5\u53ca\u4e0eDocker\u96c6\u6210\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u7ba1\u7406\u548c\u7ef4\u62a4Go\u5e94\u7528\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecdGo\u9879\u76ee\u7684\u7ec4\u7ec7\u7ed3\u6784\uff0c\u5982\u4f55\u5229\u7528Go\u521b\u5efaWeb\u670d\u52a1\uff0c\u5e76\u63a2\u8ba8Go\u5e94\u7528\u7684\u90e8\u7f72\u4e0e\u53d1\u5e03\uff0c\u7279\u522b\u662f\u5982\u4f55\u4e0eDocker\u8fdb\u884c\u96c6\u6210\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">8.1 <strong>\u9879\u76ee\u7ed3\u6784\u4e0e\u7ec4\u7ec7<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u9f13\u52b1\u7b80\u6d01\u800c\u89c4\u8303\u7684\u9879\u76ee\u7ed3\u6784\uff0c\u4e0d\u540c\u4e8e\u4e00\u4e9b\u5176\u4ed6\u8bed\u8a00\uff0cGo\u7684\u9879\u76ee\u7ec4\u7ec7\u65b9\u5f0f\u5e76\u6ca1\u6709\u7279\u522b\u590d\u6742\u7684\u8981\u6c42\uff0c\u4f46\u63a8\u8350\u9075\u5faa\u4e00\u5b9a\u7684\u7ea6\u5b9a\uff0c\u4ee5\u4fbf\u4e8e\u56e2\u961f\u534f\u4f5c\u548c\u9879\u76ee\u7684\u53ef\u7ef4\u62a4\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5e38\u89c1\u7684Go\u9879\u76ee\u7ed3\u6784\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>myapp\/\n\u251c\u2500\u2500 bin\/                # \u5b58\u653e\u7f16\u8bd1\u540e\u7684\u53ef\u6267\u884c\u6587\u4ef6\n\u251c\u2500\u2500 cmd\/                # \u4e3b\u7a0b\u5e8f\u5165\u53e3\n\u2502   \u2514\u2500\u2500 myapp\/          # \u5177\u4f53\u5e94\u7528\u7a0b\u5e8f\uff0c\u901a\u5e38\u53ea\u6709\u4e00\u4e2a\n\u251c\u2500\u2500 internal\/           # \u5185\u90e8\u5305\uff0c\u5916\u90e8\u65e0\u6cd5\u5f15\u7528\n\u2502   \u2514\u2500\u2500 user\/           # \u7528\u6237\u76f8\u5173\u7684\u903b\u8f91\n\u251c\u2500\u2500 pkg\/                # \u53ef\u4f9b\u5916\u90e8\u5f15\u7528\u7684\u5305\n\u2502   \u2514\u2500\u2500 db\/             # \u6570\u636e\u5e93\u76f8\u5173\u5305\n\u251c\u2500\u2500 api\/                # API\u5b9a\u4e49\u548c\u534f\u8bae\u76f8\u5173\n\u251c\u2500\u2500 web\/                # Web\u670d\u52a1\u76f8\u5173\u7684\u8d44\u6e90\uff0c\u5982HTML\u3001CSS\u3001JS\u7b49\n\u251c\u2500\u2500 go.mod              # Go\u6a21\u5757\u7ba1\u7406\u6587\u4ef6\n\u251c\u2500\u2500 go.sum              # Go\u6a21\u5757\u4f9d\u8d56\u4fe1\u606f\n\u2514\u2500\u2500 README.md           # \u9879\u76ee\u8bf4\u660e\u6587\u6863<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">8.1.1 <strong>\u4e3b\u8981\u76ee\u5f55\u548c\u6587\u4ef6\u8bf4\u660e<\/strong><\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>cmd\/\uff1a<\/strong> \u5305\u542b\u7a0b\u5e8f\u7684\u4e3b\u5165\u53e3\uff0c\u6bcf\u4e2a\u5e94\u7528\u7a0b\u5e8f\u90fd\u6709\u4e00\u4e2a\u5b50\u76ee\u5f55\u3002\u6bd4\u5982\u5728<code>myapp<\/code>\u9879\u76ee\u4e2d\uff0c<code>cmd\/myapp<\/code>\u76ee\u5f55\u5b58\u653e\u7684\u662f\u7a0b\u5e8f\u7684\u542f\u52a8\u4ee3\u7801\u3002<\/li>\n\n\n\n<li><strong>internal\/\uff1a<\/strong> \u5b58\u653e\u4e0d\u5e0c\u671b\u5916\u90e8\u9879\u76ee\u76f4\u63a5\u5f15\u7528\u7684\u5305\u3002Go\u8bed\u8a00\u6709\u4e00\u4e2a\u7279\u6b8a\u7684<code>internal<\/code>\u5305\uff0c\u5b83\u80fd\u786e\u4fdd\u5916\u90e8\u9879\u76ee\u65e0\u6cd5\u5f15\u7528\u5176\u4e2d\u7684\u5185\u5bb9\u3002<\/li>\n\n\n\n<li><strong>pkg\/\uff1a<\/strong> \u5b58\u653e\u53ef\u4ee5\u4f9b\u5916\u90e8\u9879\u76ee\u5f15\u7528\u7684\u5305\uff0c\u901a\u5e38\u662f\u4e00\u4e9b\u901a\u7528\u7684\u4e1a\u52a1\u903b\u8f91\u6216\u8005\u5de5\u5177\u51fd\u6570\u3002<\/li>\n\n\n\n<li><strong>api\/\uff1a<\/strong> \u5b58\u653eAPI\u76f8\u5173\u7684\u5b9a\u4e49\u548c\u534f\u8bae\u6587\u4ef6\uff0c\u901a\u5e38\u662f\u7ed3\u6784\u4f53\u7684\u5b9a\u4e49\u3001\u8bf7\u6c42\u548c\u54cd\u5e94\u6a21\u578b\u3002<\/li>\n\n\n\n<li><strong>web\/\uff1a<\/strong> \u5b58\u653e\u4e0eWeb\u670d\u52a1\u76f8\u5173\u7684\u8d44\u6e90\u6587\u4ef6\uff0c\u4f8b\u5982HTML\u6a21\u677f\u3001CSS\u6587\u4ef6\u3001JavaScript\u6587\u4ef6\u7b49\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u79cd\u7ed3\u6784\u4f7f\u5f97\u9879\u76ee\u975e\u5e38\u6e05\u6670\uff0c\u6bcf\u4e2a\u529f\u80fd\u6a21\u5757\u6709\u660e\u786e\u7684\u533a\u5206\uff0c\u4e5f\u65b9\u4fbf\u56e2\u961f\u5f00\u53d1\u3001\u6d4b\u8bd5\u548c\u7ef4\u62a4\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">8.2 <strong>\u4f7f\u7528Go\u521b\u5efaWeb\u670d\u52a1<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u4f5c\u4e3a\u540e\u7aef\u5f00\u53d1\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u53ef\u4ee5\u975e\u5e38\u8f7b\u677e\u5730\u521b\u5efa\u9ad8\u6548\u7684Web\u670d\u52a1\u3002\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u5c55\u793a\u5982\u4f55\u4f7f\u7528Go\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Web\u670d\u52a1\uff0c\u5e76\u5904\u7406\u8def\u7531\u3001\u8bf7\u6c42\u548c\u54cd\u5e94\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">8.2.1 <strong>\u57fa\u672c\u7684Web\u670d\u52a1<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528Go\u521b\u5efaWeb\u670d\u52a1\u975e\u5e38\u7b80\u5355\uff0c<code>net\/http<\/code>\u5305\u63d0\u4f9b\u4e86Web\u670d\u52a1\u5668\u7684\u57fa\u672c\u529f\u80fd\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1a\u7b80\u5355\u7684Web\u670d\u52a1<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"net\/http\"\n)\n\nfunc handler(w http.ResponseWriter, r *http.Request) {\n    fmt.Fprintf(w, \"Hello, Go Web Service!\")\n}\n\nfunc main() {\n    http.HandleFunc(\"\/\", handler) \/\/ \u8bbe\u7f6e\u8def\u7531\n    fmt.Println(\"Server started at :8080\")\n    err := http.ListenAndServe(\":8080\", nil) \/\/ \u542f\u52a8Web\u670d\u52a1\n    if err != nil {\n        fmt.Println(\"Error starting server:\", err)\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684Web\u670d\u52a1\u5668\uff0c\u76d1\u542c8080\u7aef\u53e3\uff0c\u5f53\u8bbf\u95ee\u6839\u8def\u5f84<code>\/<\/code>\u65f6\uff0c\u4f1a\u54cd\u5e94<code>Hello, Go Web Service!<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">8.2.2 <strong>\u4f7f\u7528Web\u6846\u67b6\uff08Gin\uff09\u521b\u5efaWeb\u670d\u52a1<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u867d\u7136Go\u81ea\u5e26\u7684<code>net\/http<\/code>\u5305\u5df2\u7ecf\u80fd\u591f\u5904\u7406Web\u8bf7\u6c42\uff0c\u4f46\u5f88\u591a\u5f00\u53d1\u8005\u4f1a\u9009\u62e9\u4f7f\u7528\u66f4\u4e3a\u9ad8\u6548\u548c\u7075\u6d3b\u7684Web\u6846\u67b6\uff0c\u5982<strong>Gin<\/strong>\u3002Gin\u63d0\u4f9b\u4e86\u66f4\u591a\u529f\u80fd\uff0c\u6bd4\u5982\u8def\u7531\u3001\u4e2d\u95f4\u4ef6\u652f\u6301\u3001JSON\u54cd\u5e94\u7b49\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1a\u4f7f\u7528Gin\u6846\u67b6\u521b\u5efaWeb\u670d\u52a1<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"github.com\/gin-gonic\/gin\"\n)\n\nfunc main() {\n    r := gin.Default()\n\n    \/\/ \u5b9a\u4e49GET\u8bf7\u6c42\u8def\u7531\n    r.GET(\"\/\", func(c *gin.Context) {\n        c.JSON(200, gin.H{\n            \"message\": \"Hello, Gin Web Service!\",\n        })\n    })\n\n    \/\/ \u542f\u52a8Web\u670d\u52a1\n    r.Run(\":8080\")\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Gin\u6846\u67b6\u6bd4\u539f\u751f<code>net\/http<\/code>\u5305\u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u548c\u76f4\u89c2\u7684API\uff0c\u66f4\u5bb9\u6613\u8fdb\u884c\u8def\u7531\u5904\u7406\u3001\u53c2\u6570\u7ed1\u5b9a\u548cJSON\u54cd\u5e94\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">8.3 <strong>Go\u7684\u90e8\u7f72\u4e0e\u53d1\u5e03<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u90e8\u7f72Go\u5e94\u7528\u7a0b\u5e8f\u76f8\u5bf9\u7b80\u5355\uff0c\u56e0\u4e3aGo\u662f\u7f16\u8bd1\u578b\u8bed\u8a00\uff0c\u5b83\u53ef\u4ee5\u751f\u6210\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u8fd9\u4f7f\u5f97Go\u5e94\u7528\u7a0b\u5e8f\u4e0d\u9700\u8981\u989d\u5916\u7684\u8fd0\u884c\u65f6\u73af\u5883\u3002\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5c06\u7f16\u8bd1\u540e\u7684\u53ef\u6267\u884c\u6587\u4ef6\u653e\u5230\u670d\u52a1\u5668\u4e0a\u8fd0\u884c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">8.3.1 <strong>\u6784\u5efa\u548c\u53d1\u5e03<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u5e94\u7528\u7a0b\u5e8f\u4f7f\u7528<code>go build<\/code>\u547d\u4ee4\u8fdb\u884c\u7f16\u8bd1\u3002\u7f16\u8bd1\u540e\uff0c\u53ef\u4ee5\u76f4\u63a5\u5c06\u751f\u6210\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e0a\u4f20\u5230\u76ee\u6807\u670d\u52a1\u5668\u4e0a\u8fd0\u884c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1a\u6784\u5efaGo\u5e94\u7528\u7a0b\u5e8f<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>go build -o myapp main.go<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u8ff0\u547d\u4ee4\u4f1a\u5728\u5f53\u524d\u76ee\u5f55\u751f\u6210\u4e00\u4e2a\u540d\u4e3a<code>myapp<\/code>\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u5c06\u5b83\u4e0a\u4f20\u5230\u76ee\u6807\u670d\u52a1\u5668\u5e76\u8fd0\u884c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">8.3.2 <strong>\u8de8\u5e73\u53f0\u6784\u5efa<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u652f\u6301\u8de8\u5e73\u53f0\u7f16\u8bd1\uff0c\u4f60\u53ef\u4ee5\u5728\u5f00\u53d1\u5e73\u53f0\u4e0a\u6784\u5efa\u51fa\u9002\u7528\u4e8e\u5176\u4ed6\u5e73\u53f0\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1a\u6784\u5efaWindows\u7248\u672c\u7684Go\u5e94\u7528<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GOOS=windows GOARCH=amd64 go build -o myapp.exe main.go<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u6761\u547d\u4ee4\u5c06\u6784\u5efa\u51fa\u9002\u7528\u4e8eWindows\u64cd\u4f5c\u7cfb\u7edf\u7684<code>myapp.exe<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">8.3.3 <strong>\u4f7f\u7528\u7cfb\u7edf\u670d\u52a1\u7ba1\u7406Go\u5e94\u7528<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u4f7f\u7528<code>systemd<\/code>\uff08\u5728Linux\u4e0a\uff09\u7b49\u5de5\u5177\u6765\u7ba1\u7406Go\u5e94\u7528\u7684\u8fd0\u884c\u548c\u76d1\u63a7\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>systemd<\/code>\u7ba1\u7406Go\u5e94\u7528\u7684\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1a<code>systemd<\/code>\u670d\u52a1\u5355\u5143\u914d\u7f6e<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;Unit]\nDescription=Go Web Service\nAfter=network.target\n\n&#91;Service]\nExecStart=\/path\/to\/myapp\nWorkingDirectory=\/path\/to\/\nRestart=always\nUser=nobody\nGroup=nogroup\n\n&#91;Install]\nWantedBy=multi-user.target<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c06\u8fd9\u4e2a\u914d\u7f6e\u6587\u4ef6\u4fdd\u5b58\u4e3a<code>\/etc\/systemd\/system\/myapp.service<\/code>\uff0c\u7136\u540e\u901a\u8fc7<code>systemctl<\/code>\u547d\u4ee4\u6765\u542f\u52a8\u548c\u7ba1\u7406Go\u5e94\u7528\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl start myapp\nsudo systemctl enable myapp<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">8.4 <strong>\u4f7f\u7528Docker\u4e0eGo\u5e94\u7528\u96c6\u6210<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u5e94\u7528\u7684\u4e00\u4e2a\u663e\u8457\u4f18\u52bf\u662f\u7f16\u8bd1\u540e\u7684\u53ef\u6267\u884c\u6587\u4ef6\u4e0d\u4f9d\u8d56\u5916\u90e8\u73af\u5883\uff0c\u8fd9\u4f7f\u5f97\u5b83\u975e\u5e38\u9002\u5408\u4e0eDocker\u8fdb\u884c\u96c6\u6210\u3002\u901a\u8fc7\u5c06Go\u5e94\u7528\u6253\u5305\u5230Docker\u5bb9\u5668\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u5f97\u5e94\u7528\u7684\u90e8\u7f72\u66f4\u52a0\u6807\u51c6\u5316\uff0c\u4e14\u5177\u6709\u8de8\u5e73\u53f0\u7684\u80fd\u529b\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">8.4.1 <strong>\u521b\u5efaDockerfile<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3a\u4e86\u5c06Go\u5e94\u7528\u6253\u5305\u5230Docker\u5bb9\u5668\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u7f16\u5199\u4e00\u4e2a<code>Dockerfile<\/code>\uff0c\u5b83\u5b9a\u4e49\u4e86\u5e94\u7528\u7684\u6784\u5efa\u548c\u8fd0\u884c\u65b9\u5f0f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1aGo\u5e94\u7528\u7684Dockerfile<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u4f7f\u7528\u5b98\u65b9\u7684Go\u955c\u50cf\u4f5c\u4e3a\u6784\u5efa\u73af\u5883\nFROM golang:1.18-alpine AS build\n\n# \u8bbe\u7f6e\u5de5\u4f5c\u76ee\u5f55\nWORKDIR \/app\n\n# \u5c06\u4ee3\u7801\u590d\u5236\u5230\u5bb9\u5668\u4e2d\nCOPY . .\n\n# \u4e0b\u8f7d\u4f9d\u8d56\u5e76\u6784\u5efaGo\u5e94\u7528\nRUN go mod tidy\nRUN go build -o myapp main.go\n\n# \u4f7f\u7528\u66f4\u5c0f\u7684\u955c\u50cf\u4f5c\u4e3a\u8fd0\u884c\u73af\u5883\nFROM alpine:latest\n\n# \u5b89\u88c5\u5fc5\u8981\u7684\u8fd0\u884c\u65f6\u4f9d\u8d56\nRUN apk --no-cache add ca-certificates\n\n# \u5c06\u6784\u5efa\u597d\u7684Go\u5e94\u7528\u590d\u5236\u5230\u8fd0\u884c\u73af\u5883\u4e2d\nCOPY --from=build \/app\/myapp \/usr\/local\/bin\/myapp\n\n# \u8bbe\u7f6e\u5bb9\u5668\u542f\u52a8\u547d\u4ee4\nCMD &#91;\"myapp\"]\n\n# \u66b4\u9732\u5e94\u7528\u7684\u7aef\u53e3\nEXPOSE 8080<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a<code>Dockerfile<\/code>\u5c06\u5e94\u7528\u5206\u4e3a\u4e24\u4e2a\u9636\u6bb5\uff1a\u9996\u5148\u4f7f\u7528<code>golang:1.18-alpine<\/code>\u955c\u50cf\u6784\u5efa\u5e94\u7528\uff0c\u7136\u540e\u5c06\u6784\u5efa\u597d\u7684\u53ef\u6267\u884c\u6587\u4ef6\u590d\u5236\u5230\u66f4\u5c0f\u7684<code>alpine<\/code>\u955c\u50cf\u4e2d\u3002\u8fd9\u6837\u53ef\u4ee5\u51cf\u5c0f\u6700\u7ec8\u5bb9\u5668\u7684\u4f53\u79ef\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">8.4.2 <strong>\u6784\u5efaDocker\u955c\u50cf<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u53ef\u4ee5\u6784\u5efaGo\u5e94\u7528\u7684Docker\u955c\u50cf\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker build -t myapp .<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">8.4.3 <strong>\u8fd0\u884cDocker\u5bb9\u5668<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u6784\u5efa\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7Docker\u8fd0\u884c\u5bb9\u5668\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker run -p 8080:8080 myapp<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4f1a\u542f\u52a8\u4e00\u4e2a\u5bb9\u5668\uff0c\u5c06Go\u5e94\u7528\u8fd0\u884c\u5728\u5bb9\u5668\u5185\uff0c\u5e76\u5c06\u5bb9\u5668\u76848080\u7aef\u53e3\u6620\u5c04\u5230\u4e3b\u673a\u76848080\u7aef\u53e3\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u7684\u9879\u76ee\u7ed3\u6784\u6e05\u6670\u3001\u7b80\u6d01\uff0c\u975e\u5e38\u9002\u5408\u9ad8\u6548\u7684Web\u670d\u52a1\u5f00\u53d1\u548c\u90e8\u7f72\u3002\u5728\u672c\u7ae0\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5982\u4f55\u7ec4\u7ec7Go\u9879\u76ee\u7684\u7ed3\u6784\u3001\u4f7f\u7528Go\u6784\u5efaWeb\u670d\u52a1\u3001\u5982\u4f55\u9ad8\u6548\u5730\u53d1\u5e03\u548c\u90e8\u7f72Go\u5e94\u7528\uff0c\u5e76\u901a\u8fc7Docker\u8fdb\u884c\u5bb9\u5668\u5316\u7ba1\u7406\u3002\u638c\u63e1\u4e86\u8fd9\u4e9b\u6280\u80fd\u540e\uff0c\u60a8\u53ef\u4ee5\u66f4\u8f7b\u677e\u5730\u7ba1\u7406Go\u5e94\u7528\uff0c\u786e\u4fdd\u5176\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u9ad8\u6548\u8fd0\u884c\u3002\u540c\u65f6\uff0c\u7ed3\u5408Docker\u5bb9\u5668\u5316\u6280\u672f\uff0c\u60a8\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728\u4e0d\u540c\u5e73\u53f0\u4e4b\u95f4\u8fc1\u79fb\u548c\u90e8\u7f72Go\u5e94\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Go\u8bed\u8a00\u7684\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u9879\u76ee\u7684\u7ed3\u6784\u548c\u7ec4\u7ec7\u4ee5\u53ca\u5982\u4f55\u9ad8\u6548\u5730\u90e8\u7f72\u548c\u53d1\u5e03\u5e94\u7528\u7a0b\u5e8f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u901a\u8fc7\u4e86\u89e3Go\u8bed\u8a00\u9879\u76ee\u7ed3\u6784 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58],"tags":[],"class_list":["post-1445","post","type-post","status-publish","format-standard","hentry","category-golang"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1445","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1445"}],"version-history":[{"count":2,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1445\/revisions"}],"predecessor-version":[{"id":1460,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1445\/revisions\/1460"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}