{"id":205705,"date":"2025-06-12T10:51:48","date_gmt":"2025-06-12T02:51:48","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205705\/"},"modified":"2025-06-12T10:51:48","modified_gmt":"2025-06-12T02:51:48","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-go-%e5%87%bd%e6%95%b0%e4%b8%ad%e4%bd%bf%e7%94%a8%e5%8f%8d%e5%b0%84%e4%bc%a0%e9%80%92%e4%bb%bb%e6%84%8f%e7%9a%84%e5%80%bc%e7%b1%bb%e5%9e%8b%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205705\/","title":{"rendered":"\u5982\u4f55\u5728 Go \u51fd\u6570\u4e2d\u4f7f\u7528\u53cd\u5c04\u4f20\u9012\u4efb\u610f\u7684\u503c\u7c7b\u578b\uff1f"},"content":{"rendered":"<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9GOLANG\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300a\u5982\u4f55\u5728 Go \u51fd\u6570\u4e2d\u4f7f\u7528\u53cd\u5c04\u4f20\u9012\u4efb\u610f\u7684\u503c\u7c7b\u578b\uff1f\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01,\u901a\u8fc7\u53cd\u5c04\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4efb\u610f\u503c\u7c7b\u578b\u4f20\u9012\u7ed9 Go \u51fd\u6570\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u4f7f\u7528 reflect.ValueOf() \u83b7\u53d6\u503c\u8868\u793a\u3002\u5c06\u6b64\u8868\u793a\u8f6c\u6362\u4e3a interface{} \u7c7b\u578b\u3002\u5c06\u8f6c\u6362\u540e\u7684\u503c\u4f5c\u4e3a\u51fd\u6570\u53c2\u6570\u4f20\u9012\u3002,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729946207671ce25fcf332.jpg\" class=\"aligncenter\">,<br \/>\n<strong>\u5982\u4f55\u5728 Go \u51fd\u6570\u4e2d\u4f7f\u7528\u53cd\u5c04\u4f20\u9012\u4efb\u610f\u7684\u503c\u7c7b\u578b\uff1f<\/strong>,\u53cd\u5c04\u662f Go \u8bed\u8a00\u4e2d\u4e00\u9879\u5f3a\u5927\u7684\u7279\u6027\uff0c\u5b83\u5141\u8bb8\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u68c0\u67e5\u548c\u4fee\u6539\u7a0b\u5e8f\u81ea\u8eab\u7684\u7ed3\u6784\u3002\u501f\u52a9\u53cd\u5c04\uff0c\u6211\u4eec\u53ef\u4ee5\u52a8\u6001\u5730\u83b7\u53d6\u548c\u8bbe\u7f6e\u4efb\u610f\u7c7b\u578b\u7684\u53d8\u91cf\u7684\u503c\u3002,<br \/>\n<strong>\u53cd\u5c04\u57fa\u7840<\/strong>,\u5728 Go \u4e2d\uff0c\u53cd\u5c04\u901a\u8fc7<br \/>\n<code>reflect<\/code> \u5305\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u5e38\u7528\u7684\u53cd\u5c04\u7c7b\u578b\uff1a,<br \/>\n<strong>\u5c06\u503c\u4f20\u9012\u7ed9\u51fd\u6570<\/strong>,\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u53cd\u5c04\u5c06\u4efb\u610f\u7684\u503c\u7c7b\u578b\u4f20\u9012\u7ed9\u51fd\u6570\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u9700\u8981\u4ee5\u4e0b\u6b65\u9aa4\uff1a,<br \/>\n<strong>\u5b9e\u6218\u6848\u4f8b<\/strong>,\u4e0b\u9762\u662f\u4e00\u4e2a\u5c06<br \/>\n<code>int<\/code> \u548c<br \/>\n<code>string<\/code> \u503c\u4f20\u9012\u7ed9<br \/>\n<code>MyFunc<\/code> \u51fd\u6570\u7684\u793a\u4f8b\uff1a,\u5728<br \/>\n<code>MyFunc<\/code> \u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<br \/>\n<code>reflect.Typeof()<\/code> \u548c<br \/>\n<code>reflect.Value.Type()<\/code> \u65b9\u6cd5\u6765\u68c0\u67e5\u503c\u7684\u7c7b\u578b\uff1a,\u8f93\u51fa\uff1a,\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u5728 Go \u51fd\u6570\u4e2d\u4f7f\u7528\u53cd\u5c04\u4f20\u9012\u4efb\u610f\u7684\u503c\u7c7b\u578b\uff1f\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~GOLANG\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03GOLANG\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01,<br \/>\n<code>reflect.Value<\/code>: \u8868\u793a Go \u503c\u7684\u62bd\u8c61\u3002,<br \/>\n<code>reflect.Type<\/code>: \u8868\u793a Go \u7c7b\u578b\u7684\u62bd\u8c61\u3002,\u5c0f\u4f19\u4f34\u4eec\u5bf9GOLANG\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300a\u5982\u4f55\u5728 Go \u51fd\u6570\u4e2d\u4f7f\u7528\u53cd\u5c04\u4f20\u9012\u4efb\u610f\u7684\u503c\u7c7b\u578b\uff1f\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01,<br \/>\n<b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>\u5982\u4f55\u5728 Go \u51fd\u6570\u4e2d\u4f7f\u7528\u53cd\u5c04\u4f20\u9012\u4efb\u610f\u7684\u503c\u7c7b\u578b\uff1f<\/span><\/p>\n<h1>\u5982\u4f55\u5728 Go \u51fd\u6570\u4e2d\u4f7f\u7528\u53cd\u5c04\u4f20\u9012\u4efb\u610f\u7684\u503c\u7c7b\u578b\uff1f<\/h1>\n<p><span>2024-10-26 20:37:02<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9GOLANG\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300a\u5982\u4f55\u5728 Go \u51fd\u6570\u4e2d\u4f7f\u7528\u53cd\u5c04\u4f20\u9012\u4efb\u610f\u7684\u503c\u7c7b\u578b\uff1f\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p>\u901a\u8fc7\u53cd\u5c04\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4efb\u610f\u503c\u7c7b\u578b\u4f20\u9012\u7ed9 Go \u51fd\u6570\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u4f7f\u7528 reflect.ValueOf() \u83b7\u53d6\u503c\u8868\u793a\u3002\u5c06\u6b64\u8868\u793a\u8f6c\u6362\u4e3a interface{} \u7c7b\u578b\u3002\u5c06\u8f6c\u6362\u540e\u7684\u503c\u4f5c\u4e3a\u51fd\u6570\u53c2\u6570\u4f20\u9012\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729946207671ce25fcf332.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u5728 Go \u51fd\u6570\u4e2d\u4f7f\u7528\u53cd\u5c04\u4f20\u9012\u4efb\u610f\u7684\u503c\u7c7b\u578b\uff1f<\/strong><\/p>\n<p>\u53cd\u5c04\u662f Go \u8bed\u8a00\u4e2d\u4e00\u9879\u5f3a\u5927\u7684\u7279\u6027\uff0c\u5b83\u5141\u8bb8\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u68c0\u67e5\u548c\u4fee\u6539\u7a0b\u5e8f\u81ea\u8eab\u7684\u7ed3\u6784\u3002\u501f\u52a9\u53cd\u5c04\uff0c\u6211\u4eec\u53ef\u4ee5\u52a8\u6001\u5730\u83b7\u53d6\u548c\u8bbe\u7f6e\u4efb\u610f\u7c7b\u578b\u7684\u53d8\u91cf\u7684\u503c\u3002<\/p>\n<p><strong>\u53cd\u5c04\u57fa\u7840<\/strong><\/p>\n<p>\u5728 Go \u4e2d\uff0c\u53cd\u5c04\u901a\u8fc7 <code>reflect<\/code> \u5305\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u5e38\u7528\u7684\u53cd\u5c04\u7c7b\u578b\uff1a<\/p>\n<ul>\n<li><code>reflect.Type<\/code>: \u8868\u793a Go \u7c7b\u578b\u7684\u62bd\u8c61\u3002<\/li>\n<li><code>reflect.Value<\/code>: \u8868\u793a Go \u503c\u7684\u62bd\u8c61\u3002<\/li>\n<\/ul>\n<p><strong>\u5c06\u503c\u4f20\u9012\u7ed9\u51fd\u6570<\/strong><\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u53cd\u5c04\u5c06\u4efb\u610f\u7684\u503c\u7c7b\u578b\u4f20\u9012\u7ed9\u51fd\u6570\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u9700\u8981\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\u4f7f\u7528 <code>reflect.ValueOf()<\/code> \u51fd\u6570\u83b7\u53d6\u503c\u7684 <code>reflect.Value<\/code> \u8868\u793a\u3002<\/li>\n<li>\u5c06\u6b64 <code>reflect.Value<\/code> \u8f6c\u6362\u4e3a <code>interface{}<\/code> \u7c7b\u578b\u3002<\/li>\n<li>\u5c06\u8f6c\u6362\u540e\u7684\u503c\u4f5c\u4e3a\u51fd\u6570\u7684\u53c2\u6570\u4f20\u9012\u3002<\/li>\n<\/ol>\n<pre>func MyFunc(v interface{}) {\n    \/\/ \u4f7f\u7528\u53cd\u5c04\u5904\u7406 v ...\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5c06 <code>int<\/code> \u548c <code>string<\/code> \u503c\u4f20\u9012\u7ed9 <code>MyFunc<\/code> \u51fd\u6570\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>func main() {\n    i := 42\n    s := \"Hello, world!\"\n\n    MyFunc(i)\n    MyFunc(s)\n}<\/pre>\n<p>\u5728 <code>MyFunc<\/code> \u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>reflect.Typeof()<\/code> \u548c <code>reflect.Value.Type()<\/code> \u65b9\u6cd5\u6765\u68c0\u67e5\u503c\u7684\u7c7b\u578b\uff1a<\/p>\n<pre>func MyFunc(v interface{}) {\n    t := reflect.TypeOf(v)\n\n    switch t {\n    case reflect.TypeOf(0):\n        fmt.Println(\"int:\", v)\n    case reflect.TypeOf(\"\"):\n        fmt.Println(\"string:\", v)\n    default:\n        fmt.Println(\"Unknown type:\", t)\n    }\n}<\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>int: 42\nstring: Hello, world!<\/pre>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u5728 Go \u51fd\u6570\u4e2d\u4f7f\u7528\u53cd\u5c04\u4f20\u9012\u4efb\u610f\u7684\u503c\u7c7b\u578b\uff1f\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~GOLANG\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03GOLANG\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u786c\u76d8\u5bfc\u81f4\u7535\u8111\u4e0d\u65ad\u91cd\u542f\uff1f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u5982\u4f55\u5728 Java \u4e2d\u58f0\u660e\u53d7\u4fdd\u62a4\u7684\u51fd\u6570\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9GOLANG\u7f16\u7a0b\u611f\u5174\u8da3&#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-205705","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205705","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=205705"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205705\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}