{"id":205833,"date":"2025-06-12T11:37:15","date_gmt":"2025-06-12T03:37:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205833\/"},"modified":"2025-06-12T11:37:15","modified_gmt":"2025-06-12T03:37:15","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-golang-%e5%8f%8d%e5%b0%84%e8%b0%83%e7%94%a8%e4%bb%bb%e6%84%8f%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205833\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Golang \u53cd\u5c04\u8c03\u7528\u4efb\u610f\u51fd\u6570"},"content":{"rendered":"<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4f7f\u7528 GOLANG \u53cd\u5c04\u8c03\u7528\u4efb\u610f\u51fd\u6570\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730013336671de8987ec30.jpg\" class=\"aligncenter\">,<br \/>\n<strong>\u5982\u4f55\u5728 Go \u4e2d\u4f7f\u7528\u53cd\u5c04\u8c03\u7528\u4efb\u610f\u51fd\u6570<\/strong>,\u53cd\u5c04\u662f Go \u4e2d\u5185\u7f6e\u7684\u4e00\u9879\u529f\u80fd\uff0c\u5b83\u5141\u8bb8\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u68c0\u67e5\u548c\u4fee\u6539\u81ea\u8eab\u7ed3\u6784\u3002\u5b83\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c\u65f6\u5305\u4e2d\u7684<br \/>\n<code>reflect<\/code> \u5305\u6765\u5b9e\u73b0\u3002\u5229\u7528\u53cd\u5c04\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u8c03\u7528\u4efb\u610f\u51fd\u6570\uff0c\u5e26\u6765\u5f3a\u5927\u7684\u7075\u6d3b\u6027\u3002,<br \/>\n<strong>\u57fa\u7840\u539f\u7406<\/strong>,<br \/>\n<code>reflect.Value<\/code> \u7c7b\u578b\u8868\u793a\u53cd\u5c04\u7684 Go \u503c\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u51fd\u6570\u63a5\u53e3<br \/>\n<code>Call<\/code>\uff0c\u7528\u4e8e\u8c03\u7528\u4efb\u610f\u51fd\u6570\u3002\u8981\u8c03\u7528\u4efb\u610f\u51fd\u6570\uff0c\u6211\u4eec\u9700\u8981\u505a\u7684\u5c31\u662f\u83b7\u53d6\u8be5\u51fd\u6570\u7684<br \/>\n<code>reflect.Value<\/code>\uff0c\u7136\u540e\u8c03\u7528\u5b83\u7684<br \/>\n<code>Call<\/code> \u65b9\u6cd5\u3002,<br \/>\n<strong>\u83b7\u53d6\u51fd\u6570\u7684 reflect.Value<\/strong>,\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u51fd\u6570\u7684<br \/>\n<code>reflect.Value<\/code>\uff1a,<br \/>\n<strong>\u8c03\u7528\u51fd\u6570<\/strong>,\u83b7\u53d6\u51fd\u6570\u7684<br \/>\n<code>reflect.Value<\/code> \u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u7528<br \/>\n<code>Call<\/code> \u65b9\u6cd5\u6765\u6267\u884c\u51fd\u6570\uff1a,\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<br \/>\n<code>args<\/code> \u5305\u542b\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u53c2\u6570\uff0c<br \/>\n<code>result<\/code> \u5305\u542b\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff08\u5982\u679c\u51fd\u6570\u8fd4\u56de\u4efb\u4f55\u503c\uff09\u3002,<br \/>\n<strong>\u5b9e\u6218\u793a\u4f8b<\/strong>,\u8ba9\u6211\u4eec\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u5b83\u5141\u8bb8\u7528\u6237\u5728\u8fd0\u884c\u65f6\u63d0\u4f9b\u4e00\u4e2a\u51fd\u6570\u540d\uff0c\u5e76\u7528\u53cd\u5c04\u8c03\u7528\u8be5\u51fd\u6570\uff1a,\u8fd0\u884c\u6b64\u7a0b\u5e8f\u5e76\u8f93\u5165\u51fd\u6570\u540d\uff08\u4f8b\u5982 &#8220;add&#8221; \u6216 &#8220;subtract&#8221;\uff09\u548c\u53c2\u6570\uff0c\u5b83\u5c06\u8c03\u7528\u8be5\u51fd\u6570\u5e76\u6253\u5370\u7ed3\u679c\u3002,\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGOLANG\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~,\u5982\u4f55\u5728 Go \u4e2d\u4f7f\u7528\u53cd\u5c04\u8c03\u7528\u4efb\u610f\u51fd\u6570,\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4f7f\u7528 GOLANG \u53cd\u5c04\u8c03\u7528\u4efb\u610f\u51fd\u6570\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\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\u4f7f\u7528 GOLANG \u53cd\u5c04\u8c03\u7528\u4efb\u610f\u51fd\u6570<\/span><\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 GOLANG \u53cd\u5c04\u8c03\u7528\u4efb\u610f\u51fd\u6570<\/h1>\n<p><span>2024-10-27 15:15:51<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4f7f\u7528 GOLANG \u53cd\u5c04\u8c03\u7528\u4efb\u610f\u51fd\u6570\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730013336671de8987ec30.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u5728 Go \u4e2d\u4f7f\u7528\u53cd\u5c04\u8c03\u7528\u4efb\u610f\u51fd\u6570<\/strong><\/p>\n<p>\u53cd\u5c04\u662f Go \u4e2d\u5185\u7f6e\u7684\u4e00\u9879\u529f\u80fd\uff0c\u5b83\u5141\u8bb8\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u68c0\u67e5\u548c\u4fee\u6539\u81ea\u8eab\u7ed3\u6784\u3002\u5b83\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c\u65f6\u5305\u4e2d\u7684 <code>reflect<\/code> \u5305\u6765\u5b9e\u73b0\u3002\u5229\u7528\u53cd\u5c04\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u8c03\u7528\u4efb\u610f\u51fd\u6570\uff0c\u5e26\u6765\u5f3a\u5927\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<p><strong>\u57fa\u7840\u539f\u7406<\/strong><\/p>\n<p><code>reflect.Value<\/code> \u7c7b\u578b\u8868\u793a\u53cd\u5c04\u7684 Go \u503c\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u51fd\u6570\u63a5\u53e3 <code>Call<\/code>\uff0c\u7528\u4e8e\u8c03\u7528\u4efb\u610f\u51fd\u6570\u3002\u8981\u8c03\u7528\u4efb\u610f\u51fd\u6570\uff0c\u6211\u4eec\u9700\u8981\u505a\u7684\u5c31\u662f\u83b7\u53d6\u8be5\u51fd\u6570\u7684 <code>reflect.Value<\/code>\uff0c\u7136\u540e\u8c03\u7528\u5b83\u7684 <code>Call<\/code> \u65b9\u6cd5\u3002<\/p>\n<p><strong>\u83b7\u53d6\u51fd\u6570\u7684 reflect.Value<\/strong><\/p>\n<p>\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u51fd\u6570\u7684 <code>reflect.Value<\/code>\uff1a<\/p>\n<pre>\/\/ \u6839\u636e\u51fd\u6570\u7c7b\u578b\u521b\u5efa\u4e00\u4e2a reflect.Value\nfuncval := reflect.ValueOf(func(x int) int { return x + 1 })\n\n\/\/ \u6839\u636e\u51fd\u6570\u540d\u79f0\u6216\u6307\u9488\u83b7\u53d6 reflect.Value\nfuncPtr := &amp;someFunc\nfuncval := reflect.ValueOf(funcPtr)\nfuncval = funcval.Elem()<\/pre>\n<p><strong>\u8c03\u7528\u51fd\u6570<\/strong><\/p>\n<p>\u83b7\u53d6\u51fd\u6570\u7684 <code>reflect.Value<\/code> \u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u7528 <code>Call<\/code> \u65b9\u6cd5\u6765\u6267\u884c\u51fd\u6570\uff1a<\/p>\n<pre>args := []reflect.Value{reflect.ValueOf(10)}\nresult := funcval.Call(args)<\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>args<\/code> \u5305\u542b\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u53c2\u6570\uff0c<code>result<\/code> \u5305\u542b\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff08\u5982\u679c\u51fd\u6570\u8fd4\u56de\u4efb\u4f55\u503c\uff09\u3002<\/p>\n<p><strong>\u5b9e\u6218\u793a\u4f8b<\/strong><\/p>\n<p>\u8ba9\u6211\u4eec\u5199\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u5b83\u5141\u8bb8\u7528\u6237\u5728\u8fd0\u884c\u65f6\u63d0\u4f9b\u4e00\u4e2a\u51fd\u6570\u540d\uff0c\u5e76\u7528\u53cd\u5c04\u8c03\u7528\u8be5\u51fd\u6570\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"reflect\"\n    \"strconv\"\n)\n\nfunc main() {\n    \/\/ \u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u51fd\u6570\u540d\n    var fnName string\n    fmt.Println(\"Enter the function name:\")\n    fmt.Scanf(\"%s\", &amp;fnName)\n\n    \/\/ \u901a\u8fc7\u53cd\u5c04\u627e\u5230\u51fd\u6570\n    funcValue := reflect.ValueOf(nil)\n    switch fnName {\n    case \"add\":\n        funcValue = reflect.ValueOf(func(x, y int) int { return x + y })\n    case \"subtract\":\n        funcValue = reflect.ValueOf(func(x, y int) int { return x - y })\n    }\n\n    \/\/ \u5904\u7406\u9519\u8bef\n    if funcValue.IsNil() {\n        fmt.Println(\"Invalid function name\")\n        return\n    }\n\n    \/\/ \u8f93\u5165\u53c2\u6570\n    var args []int\n    fmt.Println(\"Enter the arguments (comma-separated):\")\n    input := \"\"\n    fmt.Scanf(\"%s\", &amp;input)\n\n    \/\/ \u62c6\u5206\u8f93\u5165\u5e76\u8f6c\u6362\u6210 int\n    for _, arg := range strings.Split(input, \",\") {\n        i, err := strconv.Atoi(arg)\n        if err != nil {\n            fmt.Println(\"Invalid argument:\", err)\n            return\n        }\n        args = append(args, i)\n    }\n\n    \/\/ \u521b\u5efa\u53c2\u6570\u7684 reflect.Value \u5207\u7247\n    argValues := make([]reflect.Value, len(args))\n    for i, arg := range args {\n        argValues[i] = reflect.ValueOf(arg)\n    }\n\n    \/\/ \u8c03\u7528\u51fd\u6570\u5e76\u6253\u5370\u7ed3\u679c\n    result := funcValue.Call(argValues)\n    fmt.Println(\"Result:\", result[0].Interface())\n}<\/pre>\n<p>\u8fd0\u884c\u6b64\u7a0b\u5e8f\u5e76\u8f93\u5165\u51fd\u6570\u540d\uff08\u4f8b\u5982 &#8220;add&#8221; \u6216 &#8220;subtract&#8221;\uff09\u548c\u53c2\u6570\uff0c\u5b83\u5c06\u8c03\u7528\u8be5\u51fd\u6570\u5e76\u6253\u5370\u7ed3\u679c\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGOLANG\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Go \u6570\u7ec4\u8d4b\u503c\u7684\u8c1c\u56e2\uff1a\u4e3a\u4ec0\u4e48\u4fee\u6539\u590d\u5236\u7684\u6570\u7ec4\u4e0d\u5f71\u54cd\u539f\u6570\u7ec4\uff1f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u5982\u4f55\u4f18\u5316golang\u6846\u67b6\u4ee3\u7801\u751f\u6210\u5668\u7684\u6027\u80fd\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9&#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-205833","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205833","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=205833"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205833\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}