{"id":57896,"date":"2024-10-10T22:03:52","date_gmt":"2024-10-10T14:03:52","guid":{"rendered":"https:\/\/server.hk\/cnblog\/57896\/"},"modified":"2024-10-10T22:03:52","modified_gmt":"2024-10-10T14:03:52","slug":"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e6%a8%99%e6%ba%96%e5%ba%ab%e5%bc%b7%e5%a4%a7","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/57896\/","title":{"rendered":"GO\u6559\u7a0b: Go \u8a9e\u8a00\u6a19\u6e96\u5eab\u5f37\u5927"},"content":{"rendered":"<h1 id=\"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e6%a8%99%e6%ba%96%e5%ba%ab%e5%bc%b7%e5%a4%a7-HlJsItxvrY\">GO\u6559\u7a0b: Go \u8a9e\u8a00\u6a19\u6e96\u5eab\u5f37\u5927<\/h1>\n<p>Go \u8a9e\u8a00\uff0c\u4f5c\u70ba\u4e00\u7a2e\u975c\u614b\u985e\u578b\u3001\u7de8\u8b6f\u578b\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u81ea2009\u5e74\u7531Google\u63a8\u51fa\u4ee5\u4f86\uff0c\u8fc5\u901f\u7372\u5f97\u4e86\u958b\u767c\u8005\u7684\u9752\u775e\u3002\u5176\u7c21\u6f54\u7684\u8a9e\u6cd5\u3001\u5f37\u5927\u7684\u4e26\u767c\u652f\u6301\u4ee5\u53ca\u9ad8\u6548\u7684\u6027\u80fd\uff0c\u4f7f\u5f97Go\u8a9e\u8a00\u5728\u5f8c\u7aef\u958b\u767c\u3001\u96f2\u8a08\u7b97\u548c\u5fae\u670d\u52d9\u67b6\u69cb\u4e2d\u5f97\u5230\u4e86\u5ee3\u6cdb\u61c9\u7528\u3002\u800cGo\u8a9e\u8a00\u7684\u6a19\u6e96\u5eab\u66f4\u662f\u5176\u5f37\u5927\u4e4b\u8655\uff0c\u63d0\u4f9b\u4e86\u8c50\u5bcc\u7684\u529f\u80fd\u548c\u5de5\u5177\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u5feb\u901f\u69cb\u5efa\u9ad8\u6548\u7684\u61c9\u7528\u7a0b\u5e8f\u3002<\/p>\n<h2 id=\"go-%e8%aa%9e%e8%a8%80%e6%a8%99%e6%ba%96%e5%ba%ab%e6%a6%82%e8%bf%b0-HlJsItxvrY\">Go \u8a9e\u8a00\u6a19\u6e96\u5eab\u6982\u8ff0<\/h2>\n<p>Go \u8a9e\u8a00\u7684\u6a19\u6e96\u5eab\u5305\u542b\u4e86\u5927\u91cf\u7684\u5305\uff0c\u9019\u4e9b\u5305\u6db5\u84cb\u4e86\u5f9e\u57fa\u672c\u7684\u6578\u64da\u7d50\u69cb\u5230\u7db2\u7d61\u7de8\u7a0b\u3001\u52a0\u5bc6\u3001\u6587\u4ef6\u8655\u7406\u7b49\u591a\u500b\u65b9\u9762\u3002\u958b\u767c\u8005\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u9019\u4e9b\u6a19\u6e96\u5eab\u4f86\u5be6\u73fe\u5404\u7a2e\u529f\u80fd\uff0c\u800c\u7121\u9700\u4f9d\u8cf4\u7b2c\u4e09\u65b9\u5eab\uff0c\u9019\u4e0d\u50c5\u63d0\u9ad8\u4e86\u958b\u767c\u6548\u7387\uff0c\u4e5f\u6e1b\u5c11\u4e86\u6f5b\u5728\u7684\u5b89\u5168\u98a8\u96aa\u3002<\/p>\n<h2 id=\"%e5%b8%b8%e7%94%a8%e7%9a%84%e6%a8%99%e6%ba%96%e5%ba%ab%e5%8c%85-HlJsItxvrY\">\u5e38\u7528\u7684\u6a19\u6e96\u5eab\u5305<\/h2>\n<h3 id=\"1-fmt-HlJsItxvrY\">1. fmt<\/h3>\n<p>fmt\u5305\u63d0\u4f9b\u4e86\u683c\u5f0f\u5316\u8f38\u5165\u548c\u8f38\u51fa\u7684\u529f\u80fd\u3002\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528\u5b83\u4f86\u6253\u5370\u4fe1\u606f\u6216\u8b80\u53d6\u7528\u6236\u8f38\u5165\u3002<\/p>\n<pre><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    fmt.Println(\"Hello, World!\")\n}\n<\/code><\/pre>\n<h3 id=\"2-net-http-HlJsItxvrY\">2. net\/http<\/h3>\n<p>net\/http\u5305\u662fGo\u8a9e\u8a00\u4e2d\u7528\u65bc\u69cb\u5efaHTTP\u5ba2\u6236\u7aef\u548c\u670d\u52d9\u5668\u7684\u6838\u5fc3\u5305\u3002\u5b83\u4f7f\u5f97\u958b\u767cRESTful API\u8b8a\u5f97\u7c21\u55ae\u800c\u9ad8\u6548\u3002<\/p>\n<pre><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, World!\")\n}\n\nfunc main() {\n    http.HandleFunc(\"\/\", handler)\n    http.ListenAndServe(\":8080\", nil)\n}\n<\/code><\/pre>\n<h3 id=\"3-encoding-json-HlJsItxvrY\">3. encoding\/json<\/h3>\n<p>encoding\/json\u5305\u63d0\u4f9b\u4e86JSON\u7de8\u78bc\u548c\u89e3\u78bc\u7684\u529f\u80fd\uff0c\u9019\u5c0d\u65bc\u8655\u7406API\u6578\u64da\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<pre><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n)\n\ntype Person struct {\n    Name string `json:\"name\"`\n    Age  int    `json:\"age\"`\n}\n\nfunc main() {\n    p := Person{Name: \"Alice\", Age: 30}\n    data, _ := json.Marshal(p)\n    fmt.Println(string(data))\n}\n<\/code><\/pre>\n<h2 id=\"go-%e8%aa%9e%e8%a8%80%e6%a8%99%e6%ba%96%e5%ba%ab%e7%9a%84%e5%84%aa%e5%8b%a2-HlJsItxvrY\">Go \u8a9e\u8a00\u6a19\u6e96\u5eab\u7684\u512a\u52e2<\/h2>\n<ul>\n<li><strong>\u9ad8\u6548\u6027\uff1a<\/strong>Go \u8a9e\u8a00\u7684\u6a19\u6e96\u5eab\u7d93\u904e\u7cbe\u5fc3\u8a2d\u8a08\uff0c\u6027\u80fd\u512a\u8d8a\uff0c\u80fd\u5920\u6eff\u8db3\u9ad8\u4e26\u767c\u7684\u9700\u6c42\u3002<\/li>\n<li><strong>\u5b89\u5168\u6027\uff1a<\/strong>\u4f7f\u7528\u6a19\u6e96\u5eab\u53ef\u4ee5\u6e1b\u5c11\u4f9d\u8cf4\u7b2c\u4e09\u65b9\u5eab\u6240\u5e36\u4f86\u7684\u5b89\u5168\u98a8\u96aa\u3002<\/li>\n<li><strong>\u6613\u7528\u6027\uff1a<\/strong>\u6a19\u6e96\u5eab\u7684\u6587\u6a94\u5b8c\u5584\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u4e0a\u624b\uff0c\u5feb\u901f\u627e\u5230\u6240\u9700\u7684\u529f\u80fd\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-HlJsItxvrY\">\u7d50\u8ad6<\/h2>\n<p>Go \u8a9e\u8a00\u7684\u6a19\u6e96\u5eab\u70ba\u958b\u767c\u8005\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u5de5\u5177\uff0c\u7121\u8ad6\u662f\u5728\u69cb\u5efa\u7db2\u7d61\u61c9\u7528\u3001\u8655\u7406\u6578\u64da\u9084\u662f\u5be6\u73fe\u4e26\u767c\u64cd\u4f5c\u65b9\u9762\uff0c\u90fd\u80fd\u5920\u9ad8\u6548\u5730\u5b8c\u6210\u4efb\u52d9\u3002\u96a8\u8457Go\u8a9e\u8a00\u7684\u666e\u53ca\uff0c\u638c\u63e1\u5176\u6a19\u6e96\u5eab\u7684\u4f7f\u7528\u5c07\u6210\u70ba\u6bcf\u4f4d\u958b\u767c\u8005\u7684\u91cd\u8981\u6280\u80fd\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u90e8\u7f72\u60a8\u7684Go\u61c9\u7528\uff0cServer.HK\u63d0\u4f9b\u4e86\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u5ba2\u6236\u3002\u7121\u8ad6\u662f\u521d\u5275\u4f01\u696d\u9084\u662f\u5927\u578b\u4f01\u696d\uff0c\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u670d\u52d9\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u53ef\u9760\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22 Go \u8a9e\u8a00\u6a19\u6e96\u5eab\u7684\u5f37\u5927\u529f\u80fd\uff0c\u5b78\u7fd2\u5982\u4f55\u9ad8\u6548\u5229\u7528\u5176\u5de5\u5177\u548c\u51fd\u5f0f\uff0c\u63d0\u5347\u4f60\u7684\u958b\u767c\u6280\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-57896","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57896","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=57896"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57896\/revisions"}],"predecessor-version":[{"id":57897,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57896\/revisions\/57897"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=57896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=57896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=57896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}