{"id":207442,"date":"2025-07-08T12:03:50","date_gmt":"2025-07-08T04:03:50","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207442\/"},"modified":"2025-07-08T12:03:50","modified_gmt":"2025-07-08T04:03:50","slug":"%e5%9c%a8%e6%b2%a1%e6%9c%89%e7%b1%bb%e5%9e%8b%e6%96%ad%e8%a8%80%e7%9a%84%e6%83%85%e5%86%b5%e4%b8%8b%e6%89%93%e5%bc%80%e6%8e%a5%e5%8f%a3%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207442\/","title":{"rendered":"\u5728\u6ca1\u6709\u7c7b\u578b\u65ad\u8a00\u7684\u60c5\u51b5\u4e0b\u6253\u5f00\u63a5\u53e3\u7c7b\u578b"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5728\u6ca1\u6709\u7c7b\u578b\u65ad\u8a00\u7684\u60c5\u51b5\u4e0b\u6253\u5f00\u63a5\u53e3\u7c7b\u578b<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-22 17:27:33<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5728\u6ca1\u6709\u7c7b\u578b\u65ad\u8a00\u7684\u60c5\u51b5\u4e0b\u6253\u5f00\u63a5\u53e3\u7c7b\u578b\u300b<\/span>\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span><\/span>\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6709\u4e00\u4e2a\u53ef\u4ee5\u91c7\u7528\u591a\u79cd\u4e0d\u540c\u53c2\u6570\u7c7b\u578b\u7684\u51fd\u6570\u3002\u6211\u60f3\u4f7f\u7528\u7c7b\u578b\u5f00\u5173\u5e76\u5c3d\u53ef\u80fd\u51cf\u5c11\u4ee3\u7801\u91cd\u590d\u3002\u4f5c\u4e3a\u4e00\u4e2a\u975e\u5e38\u57fa\u672c\u7684\u793a\u4f8b\uff0c\u8fd9\u91cc\u6211\u60f3\u5c06 <code>uint8<\/code> \u548c <code>int8<\/code> \u7c7b\u578b\u590d\u5236\u5230\u5b57\u8282\u7f13\u51b2\u533a\u4e2d\u3002\u8fd9\u6bb5\u4ee3\u7801\u53ef\u4ee5\u6109\u5feb\u5730\u5de5\u4f5c<\/p>\n<pre>package main\n\nfunc switchfn(args ...interface{}) {\n    var buf []byte\n    for _, arg := range args {\n        switch val := arg.(type) {\n        case uint8:\n            buf = append(buf, byte(val))\n        case int8:\n            buf = append(buf, byte(val))\n        }\n    }\n}\n\nfunc main() {\n    switchfn(int8(42), uint8(42)) \/\/ etc\n}\n<\/pre>\n<p>\u60a8\u4f1a\u6ce8\u610f\u5230\u4e24\u4e2a case \u8bed\u53e5\u6267\u884c\u5b8c\u5168\u76f8\u540c\u7684\u64cd\u4f5c\uff01\u5982\u679c\u6211\u628a\u5b83\u4eec\u7ed3\u5408\u8d77\u6765&#8230;&#8230;<\/p>\n<pre>package main\n\nfunc switchFn(args ...interface{}) {\n    var buf []byte\n    for _, arg := range args {\n        switch val := arg.(type) {\n        case uint8, int8:\n            buf = append(buf, byte(val))\n        }\n    }\n}\n\nfunc main() {\n    switchFn(int8(42), uint8(42)) \/\/ etc\n}\n<\/pre>\n<p>\u6211\u9047\u5230\u4e86 <code>cannot conversion val (type interface {}) to type byte: need type assertion<\/code> \u7684\u95ee\u9898\u3002\u4f46\u6211\u786e\u5b9e\u6b63\u5728\u5207\u6362\u7c7b\u578b\uff01\u554a\uff01<\/p>\n<p>\u6211\u662f\u5426\u575a\u6301\u8fd9\u91cc\u7684\u4ee3\u7801\u91cd\u590d\uff0c\u6216\u8005\u662f\u5426\u6709\u66f4\u806a\u660e\u7684\u65b9\u6cd5\u6765\u505a\u5230\u8fd9\u4e00\u70b9\uff1f\u8bf7\u6ce8\u610f\uff0c<em>\u590d\u5236\u5230\u5b57\u8282\u7f13\u51b2\u533a<\/em>\u7528\u4e8e\u8bf4\u660e\u793a\u4f8b\uff0c\u6211\u7684\u51fd\u6570\u53ef\u80fd\u5728 case \u5757\u4e2d\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\u3002<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u8fd9\u4e9b\u60c5\u51b5\u53ef\u4ee5\u7ec4\u5408\uff0c\u4f46 <code>val<\/code> \u5728\u5757\u4e2d\u5c06\u5177\u6709 <code>interface{}<\/code> \u7c7b\u578b\u3002\u8fd9\u5bf9\u60a8\u7684\u573a\u666f\u6ca1\u6709\u7528\u3002<\/p>\n<p>\u4f7f\u7528\u51fd\u6570\u6765\u51cf\u5c11\u4ee3\u7801\u91cd\u590d\u3002<\/p>\n<pre>func switchfn(args ...interface{}) {\n    var buf []byte\n\n    bytefn := func(b byte) {\n        buf = append(buf, b)\n    }\n\n    for _, arg := range args {\n        switch val := arg.(type) {\n        case uint8:\n            bytefn(val)\n        case int8:\n            bytefn(byte(val))\n        }\n    }\n}<\/pre>\n<p><code>reflect<\/code> api \u4e0d\u4f1a\u6709\u5e2e\u52a9\uff0c\u56e0\u4e3a\u6709\u7b26\u53f7\u548c\u65e0\u7b26\u53f7\u503c\u9700\u8981\u5355\u72ec\u7684\u4ee3\u7801\u3002 <code>reflect<\/code> api \u6709\u52a9\u4e8e\u5c06\u6240\u6709\u6709\u7b26\u53f7\u6574\u6570\u7ec4\u5408\u5230\u4ee3\u7801\u5757\u4e2d\uff0c\u5e76\u5c06\u6240\u6709\u65e0\u7b26\u53f7\u6574\u6570\u7ec4\u5408\u5230\u53e6\u4e00\u4e2a\u4ee3\u7801\u5757\u4e2d\u3002<\/p>\n<pre>for _, arg := range args {\n    switch val := arg.(type) {\n    case int, int8, int16, int32, int64:\n        i := reflect.valueof(val).int()\n        \/\/ i is an int64\n        fmt.println(i)\n    case uint, uint8, uint16, uint32, uint64:\n        u := reflect.valueof(val).uint()\n        \/\/ u is an uint64\n        fmt.println(u)\n    }\n}<\/pre>\n<p>\u8fd9\u91cc\u6709\u4e00\u79cd\u907f\u514d\u4ee3\u7801\u91cd\u590d\u7684\u65b9\u6cd5\uff0c\u4f46\u4ee3\u4ef7\u662f\u2026\u2026\u55ef\uff0c\u53e6\u4e00\u79cd\u4ee3\u7801\u91cd\u590d\uff1a<\/p>\n<pre>func switchfn(args ...interface{}) {\n    var buf []byte\n    for _, arg := range args {\n        var val byte\n        switch v := arg.(type) {\n        case uint8:\n            val = byte(v)\n        case int8:\n            val = byte(v)\n        default:\n            panic(\"wrong type\")\n        }\n        buf = append(buf, val)\n    }\n}\n<\/pre>\n<p>\u5bf9\u4e8e\u8fd9\u4e2a\u7279\u5b9a\u7684\u51fd\u6570\uff0c\u539f\u59cb\u7684\u590d\u5236\u53ef\u80fd\u66f4\u597d\u3002\u5982\u679c <code>buf =append(buf, val)<\/code> \u90e8\u5206\u53d8\u5f97\u66f4\u5927\u6216\u66f4\u590d\u6742\uff0c\u8fd9\u53ef\u80fd\u4f1a\u66f4\u597d\u3002<\/p>\n<p>\u5728\u5176\u4ed6\u60c5\u51b5\u4e0b\uff08\u4e5f\u8bb8\u662f\u6700\u771f\u5b9e\u7684\u60c5\u51b5\uff09\uff0c\u65b9\u6cd5 \u53ef\u80fd\u662f\u6700\u597d\u7684\uff1a<\/p>\n<pre>    f := func(val byte) {\n        buffer = append(buffer, val)\n    }\n<\/pre>\n<p>\u60a8\u73b0\u5728\u53ef\u4ee5\u4ece\u6bcf\u4e2a <code>case<\/code> \u8c03\u7528 <code>f<\/code>\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u5728\u6ca1\u6709\u7c7b\u578b\u65ad\u8a00\u7684\u60c5\u51b5\u4e0b\u6253\u5f00\u63a5\u53e3\u7c7b\u578b\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591aGolang\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#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-207442","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207442","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=207442"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207442\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}