{"id":207630,"date":"2025-07-08T11:51:42","date_gmt":"2025-07-08T03:51:42","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207630\/"},"modified":"2025-07-08T11:51:42","modified_gmt":"2025-07-08T03:51:42","slug":"golang%ef%bc%9a%e5%a6%82%e4%bd%95%e6%a3%80%e6%9f%a5%e7%95%8c%e9%9d%a2%e4%b8%ad%e7%9a%84%e5%af%bc%e5%87%ba%e5%ad%97%e6%ae%b5","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207630\/","title":{"rendered":"golang\uff1a\u5982\u4f55\u68c0\u67e5\u754c\u9762\u4e2d\u7684\u5bfc\u51fa\u5b57\u6bb5"},"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>golang\uff1a\u5982\u4f55\u68c0\u67e5\u754c\u9762\u4e2d\u7684\u5bfc\u51fa\u5b57\u6bb5<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-24 09:36:43<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300agolang\uff1a\u5982\u4f55\u68c0\u67e5\u754c\u9762\u4e2d\u7684\u5bfc\u51fa\u5b57\u6bb5\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p><strong>\u7b80\u77ed\u7248\u672c\uff1a<\/strong><\/p>\n<pre>import (\n    \"fmt\"\n    \"reflect\"\n)\n\ntype StatusVal int\ntype Foo struct {\n    Name string\n    Age  int\n    art  string\n}\n\nfunc ListFields(a interface{}) {\n    v := reflect.ValueOf(a).Elem()\n    fmt.Printf(\" Kind: %+v \\n\", v.Type())\n    for _, f := range reflect.VisibleFields(v.Type()) {\n        if f.IsExported() {\n            fmt.Printf(\" Kind: %+v \\n\", f)\n        } else {\n            fmt.Printf(\" Kind: %s \\n\", f.Name)\n        }\n    }\n\n}\n\nfunc main() {\n    var x Foo\n    ListFields(&amp;x)\n\n}<\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u53ef\u4ee5\u5de5\u4f5c\uff0c\u4f46\u6211\u771f\u6b63\u9700\u8981\u7684\u662f <code>var x []foo<\/code> \uff0c\u800c\u4e14\u6211\u627e\u4e0d\u5230\u4e00\u79cd\u65b9\u6cd5\u8ba9\u5b83\u5de5\u4f5c\u5e76\u68c0\u67e5\u7ed3\u6784\u5207\u7247\u7684\u5b57\u6bb5\u4e2d\u7684 isexported \u3002<\/p>\n<p><strong>\u957f\u7248\uff1a<\/strong><\/p>\n<p>\u6211\u6b63\u5728\u5bfb\u627e\u751f\u6210\u4e00\u4e2a\u901a\u7528\u7684 sqltostruct \u51fd\u6570\uff0c\u5e76\u4e14\u6211\u9047\u5230\u4e86\u8fd9\u4e2a\u5f88\u68d2\u7684\u7b54\u6848\uff1a \u5728 go \u4e2d\u63a8\u5e7f *sql.rows scan<\/p>\n<p>\u6211\u6ca1\u6709\u8d44\u683c\u5728\u90a3\u91cc\u56de\u590d\u3002<\/p>\n<p>\u552f\u4e00\u7684\u95ee\u9898\u662f\uff0c\u5982\u679c\u6211\u7684\u7ed3\u6784\u6709\u4efb\u4f55\u672a\u5bfc\u51fa\u7684\u5b57\u6bb5\uff0c\u5b83\u4f1a\u53d1\u751f\u6050\u614c\uff0c\u6211\u60f3\u68c0\u67e5\u5b83\u5e76\u8fd4\u56de\u4e00\u4e2a\u8981\u5904\u7406\u7684\u9519\u8bef\uff0c\u800c\u4e0d\u662f\u8ba9\u5b83\u6050\u614c\u3002<\/p>\n<p>\u53e6\u5916\uff1a\u6211\u5728 go \u4e2d\u7f16\u7801\u7684\u65f6\u95f4\u5f88\u77ed\uff0c\u8bf7\u7406\u89e3\u6211\u7684\u51fa\u53d1\u70b9\uff0c\u5982\u679c\u6211\u9057\u6f0f\u4e86\u4e00\u4e9b\u7edd\u5bf9\u660e\u663e\u7684\u4e1c\u897f\uff0c\u8bf7\u62b1\u6b49\u3002<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<pre>func ListFields(a interface{}) {\n    rt := reflect.TypeOf(a) \/\/ take type of a\n    if rt.Kind() == reflect.Ptr {\n        rt = rt.Elem() \/\/ use Elem to get the pointed-to-type\n    }\n    if rt.Kind() == reflect.Slice {\n        rt = rt.Elem() \/\/ use Elem to get type of slice's element\n    }\n    if rt.Kind() == reflect.Ptr { \/\/ handle input of type like []*StructType\n        rt = rt.Elem() \/\/ use Elem to get the pointed-to-type\n    }\n    if rt.Kind() != reflect.Struct {\n        return\n    }\n\n    fmt.Printf(\" Kind: %+v \\n\", rt)\n    for _, f := range reflect.VisibleFields(rt) {\n        if f.IsExported() {\n            fmt.Printf(\" Kind: %+v \\n\", f)\n        } else {\n            fmt.Printf(\" Kind: %s \\n\", f.Name)\n        }\n    }\n}\n<\/pre>\n<\/p>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300agolang\uff1a\u5982\u4f55\u68c0\u67e5\u754c\u9762\u4e2d\u7684\u5bfc\u51fa\u5b57\u6bb5\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/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-207630","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207630","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=207630"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207630\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}