{"id":57612,"date":"2024-10-10T19:42:14","date_gmt":"2024-10-10T11:42:14","guid":{"rendered":"https:\/\/server.hk\/cnblog\/57612\/"},"modified":"2024-10-10T19:42:14","modified_gmt":"2024-10-10T11:42:14","slug":"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e9%80%9a%e9%81%8e%e5%8f%8d%e5%b0%84%e8%a8%aa%e5%95%8f%e7%b5%90%e6%a7%8b%e9%ab%94%e6%88%90%e5%93%a1%e7%9a%84%e5%80%bc","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/57612\/","title":{"rendered":"GO\u6559\u7a0b: Go \u8a9e\u8a00\u901a\u904e\u53cd\u5c04\u8a2a\u554f\u7d50\u69cb\u9ad4\u6210\u54e1\u7684\u503c"},"content":{"rendered":"<h1 id=\"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e9%80%9a%e9%81%8e%e5%8f%8d%e5%b0%84%e8%a8%aa%e5%95%8f%e7%b5%90%e6%a7%8b%e9%ab%94%e6%88%90%e5%93%a1%e7%9a%84%e5%80%bc-AeCmPnmSik\">GO\u6559\u7a0b: Go \u8a9e\u8a00\u901a\u904e\u53cd\u5c04\u8a2a\u554f\u7d50\u69cb\u9ad4\u6210\u54e1\u7684\u503c<\/h1>\n<p>\u5728Go\u8a9e\u8a00\u4e2d\uff0c\u53cd\u5c04\u662f\u4e00\u500b\u5f37\u5927\u7684\u7279\u6027\uff0c\u5141\u8a31\u7a0b\u5e8f\u5728\u904b\u884c\u6642\u6aa2\u67e5\u985e\u578b\u548c\u7d50\u69cb\u9ad4\u7684\u6210\u54e1\u3002\u9019\u5c0d\u65bc\u52d5\u614b\u8655\u7406\u6578\u64da\u7d50\u69cb\u975e\u5e38\u6709\u7528\uff0c\u7279\u5225\u662f\u5728\u9700\u8981\u901a\u7528\u6027\u548c\u9748\u6d3b\u6027\u7684\u60c5\u6cc1\u4e0b\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528\u53cd\u5c04\u4f86\u8a2a\u554f\u7d50\u69cb\u9ad4\u6210\u54e1\u7684\u503c\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u793a\u4f8b\u4ee3\u78bc\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e5%8f%8d%e5%b0%84%ef%bc%9f-AeCmPnmSik\">\u4ec0\u9ebc\u662f\u53cd\u5c04\uff1f<\/h2>\n<p>\u53cd\u5c04\u662fGo\u8a9e\u8a00\u4e2d\u7684\u4e00\u500b\u5305\uff0c\u4e3b\u8981\u7528\u65bc\u6aa2\u67e5\u985e\u578b\u548c\u7d50\u69cb\u9ad4\u7684\u5c6c\u6027\u3002\u901a\u904e\u53cd\u5c04\uff0c\u6211\u5011\u53ef\u4ee5\u5728\u904b\u884c\u6642\u7372\u53d6\u985e\u578b\u4fe1\u606f\uff0c\u4e26\u4e14\u53ef\u4ee5\u52d5\u614b\u5730\u8b80\u53d6\u548c\u4fee\u6539\u7d50\u69cb\u9ad4\u7684\u6210\u54e1\u3002\u9019\u5728\u8a31\u591a\u60c5\u6cc1\u4e0b\u90fd\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\u5728\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u6578\u64da\u6642\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8%e5%8f%8d%e5%b0%84%e8%a8%aa%e5%95%8f%e7%b5%90%e6%a7%8b%e9%ab%94%e6%88%90%e5%93%a1-AeCmPnmSik\">\u4f7f\u7528\u53cd\u5c04\u8a2a\u554f\u7d50\u69cb\u9ad4\u6210\u54e1<\/h2>\n<p>\u8981\u4f7f\u7528\u53cd\u5c04\u8a2a\u554f\u7d50\u69cb\u9ad4\u7684\u6210\u54e1\uff0c\u6211\u5011\u9700\u8981\u5c0e\u5165Go\u7684\u53cd\u5c04\u5305\uff1a<\/p>\n<pre><code>import \"reflect\"<\/code><\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u5c07\u5275\u5efa\u4e00\u500b\u7c21\u55ae\u7684\u7d50\u69cb\u9ad4\uff0c\u4e26\u4f7f\u7528\u53cd\u5c04\u4f86\u8a2a\u554f\u5176\u6210\u54e1\u7684\u503c\u3002<\/p>\n<h3 id=\"%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a2%bc-AeCmPnmSik\">\u793a\u4f8b\u4ee3\u78bc<\/h3>\n<pre><code>package main\n\nimport (\n    \"fmt\"\n    \"reflect\"\n)\n\ntype Person struct {\n    Name string\n    Age  int\n}\n\nfunc main() {\n    p := Person{Name: \"Alice\", Age: 30}\n    \n    \/\/ \u7372\u53d6\u53cd\u5c04\u503c\n    v := reflect.ValueOf(p)\n\n    \/\/ \u7372\u53d6\u7d50\u69cb\u9ad4\u985e\u578b\n    t := v.Type()\n\n    \/\/ \u904d\u6b77\u7d50\u69cb\u9ad4\u6210\u54e1\n    for i := 0; i &lt; v.NumField(); i++ {\n        \/\/ \u7372\u53d6\u6210\u54e1\u540d\u7a31\u548c\u503c\n        field := t.Field(i)\n        value := v.Field(i)\n\n        fmt.Printf(&quot;Field Name: %s, Field Value: %vn&quot;, field.Name, value.Interface())\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba`Person`\u7684\u7d50\u69cb\u9ad4\uff0c\u4e26\u5275\u5efa\u4e86\u4e00\u500b`Person`\u7684\u5be6\u4f8b\u3002\u7136\u5f8c\uff0c\u6211\u5011\u4f7f\u7528`reflect.ValueOf`\u4f86\u7372\u53d6\u8a72\u5be6\u4f8b\u7684\u53cd\u5c04\u503c\u3002\u63a5\u8457\uff0c\u6211\u5011\u904d\u6b77\u7d50\u69cb\u9ad4\u7684\u6240\u6709\u6210\u54e1\uff0c\u4e26\u6253\u5370\u51fa\u6bcf\u500b\u6210\u54e1\u7684\u540d\u7a31\u548c\u5c0d\u61c9\u7684\u503c\u3002<\/p>\n<h2 id=\"%e5%8f%8d%e5%b0%84%e7%9a%84%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-AeCmPnmSik\">\u53cd\u5c04\u7684\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u96d6\u7136\u53cd\u5c04\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u529f\u80fd\uff0c\u4f46\u5728\u4f7f\u7528\u6642\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li><strong>\u6027\u80fd\u958b\u92b7\uff1a<\/strong>\u53cd\u5c04\u7684\u6027\u80fd\u901a\u5e38\u4e0d\u5982\u76f4\u63a5\u8a2a\u554f\u7d50\u69cb\u9ad4\u6210\u54e1\uff0c\u56e0\u6b64\u5728\u6027\u80fd\u654f\u611f\u7684\u5834\u666f\u4e2d\u61c9\u8b39\u614e\u4f7f\u7528\u3002<\/li>\n<li><strong>\u985e\u578b\u5b89\u5168\uff1a<\/strong>\u53cd\u5c04\u6703\u5c0e\u81f4\u985e\u578b\u5b89\u5168\u6027\u964d\u4f4e\uff0c\u56e0\u70ba\u5728\u904b\u884c\u6642\u624d\u6703\u6aa2\u67e5\u985e\u578b\uff0c\u9019\u53ef\u80fd\u6703\u5c0e\u81f4\u904b\u884c\u6642\u932f\u8aa4\u3002<\/li>\n<li><strong>\u53ef\u8b80\u6027\uff1a<\/strong>\u904e\u5ea6\u4f7f\u7528\u53cd\u5c04\u53ef\u80fd\u6703\u4f7f\u4ee3\u78bc\u8b8a\u5f97\u96e3\u4ee5\u7406\u89e3\uff0c\u61c9\u8a72\u5728\u5fc5\u8981\u6642\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-AeCmPnmSik\">\u7d50\u8ad6<\/h2>\n<p>\u53cd\u5c04\u662fGo\u8a9e\u8a00\u4e2d\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u7279\u6027\uff0c\u80fd\u5920\u5728\u904b\u884c\u6642\u52d5\u614b\u8a2a\u554f\u7d50\u69cb\u9ad4\u7684\u6210\u54e1\u3002\u901a\u904e\u672c\u6587\u7684\u793a\u4f8b\uff0c\u60a8\u61c9\u8a72\u80fd\u5920\u7406\u89e3\u5982\u4f55\u4f7f\u7528\u53cd\u5c04\u4f86\u7372\u53d6\u7d50\u69cb\u9ad4\u7684\u6210\u54e1\u503c\u3002\u96d6\u7136\u53cd\u5c04\u63d0\u4f9b\u4e86\u9748\u6d3b\u6027\uff0c\u4f46\u5728\u4f7f\u7528\u6642\u4e5f\u8981\u8003\u616e\u6027\u80fd\u548c\u53ef\u8b80\u6027\u7b49\u56e0\u7d20\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bc\u5982\u4f55\u5728\u9999\u6e2f\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u6709\u8208\u8da3\uff0c\u6b61\u8fce\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528 Go \u8a9e\u8a00\u7684\u53cd\u5c04\u529f\u80fd\u4f86\u8a2a\u554f\u7d50\u69cb\u9ad4\u6210\u54e1\u7684\u503c\uff0c\u63d0\u5347\u4f60\u7684\u7de8\u7a0b\u6280\u5de7\u548c\u9748\u6d3b\u6027\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-57612","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57612","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=57612"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57612\/revisions"}],"predecessor-version":[{"id":57613,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57612\/revisions\/57613"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=57612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=57612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=57612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}