{"id":7063,"date":"2022-02-14T14:27:00","date_gmt":"2022-02-14T06:27:00","guid":{"rendered":"https:\/\/blog.ywdevops.cn\/?p=7063"},"modified":"2022-10-01T14:42:11","modified_gmt":"2022-10-01T06:42:11","slug":"go-13","status":"publish","type":"post","link":"https:\/\/blog.ywdevops.cn\/index.php\/2022\/02\/14\/go-13\/","title":{"rendered":"Go\u8bed\u8a00\u4e4b\u53cd\u5c04"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u53cd\u5c04\u662f\u6307\u5728\u7a0b\u5e8f\u8fd0\u884c\u671f\u5bf9\u7a0b\u5e8f\u672c\u8eab\u8fdb\u884c\u8bbf\u95ee\u548c\u4fee\u6539\u7684\u80fd\u529b\uff0c\u7a0b\u5e8f\u5728\u7f16\u8bd1\u65f6\uff0c\u53d8\u91cf\u88ab\u8f6c\u6362\u4e3a\u5185\u5b58\u5730\u5740\uff0c\u53d8\u91cf\u540d\u4e0d\u4f1a\u88ab\u7f16\u8bd1\u5668\u5199\u5165\u5230\u53ef\u6267\u884c\u90e8\u5206\uff0c\u5728\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0c\u7a0b\u5e8f\u65e0\u6cd5\u83b7\u53d6\u81ea\u8eab\u7684\u4fe1\u606f\u3002\u652f\u6301\u53cd\u5c04\u7684\u8bed\u8a00\u53ef\u4ee5\u5728\u7a0b\u5e8f\u7f16\u8bd1\u671f\u5c06\u53d8\u91cf\u7684\u53cd\u5c04\u4fe1\u606f\uff0c\u5982\u5b57\u6bb5\u540d\u79f0\u3001\u7c7b\u578b\u4fe1\u606f\u3001\u7ed3\u6784\u4f53\u4fe1\u606f\u7b49\u6574\u5408\u5230\u53ef\u6267\u884c\u6587\u4ef6\u4e2d\uff0c \u5e76\u7ed9\u7a0b\u5e8f\u63d0\u4f9b\u63a5\u53e3\u8bbf\u95ee\u53cd\u5c04\u4fe1\u606f\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u671f\u83b7\u53d6\u7c7b\u578b\u7684\u53cd\u5c04\u4fe1\u606f\uff0c\u4e95\u4e14\u6709\u80fd\u529b\u4fee\u6539\u5b83\u4eec<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Go \u7a0b\u5e8f\u5728\u8fd0\u884c\u671f\u4f7f\u7528reflect \u5305\u8bbf\u95ee\u7a0b\u5e8f\u7684\u53cd\u5c04\u4fe1\u606f<\/strong><\/p>\n\n\n\n<p class=\"has-normal-font-size wp-block-paragraph\">\u4e00<strong>\u3001\u53cd\u5c04\u7684\u7c7b\u578b\u5bf9\u8c61<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728Go \u7a0b\u5e8f\u4e2d\uff0c\u4f7f\u7528reflect. TypeOf()\u51fd\u6570\u53ef\u4ee5\u83b7\u5f97\u4efb\u610f\u503c\u7684\u7c7b\u578b\u5bf9\u8c61(reflect.Type)\uff0c\u7a0b\u5e8f\u901a\u8fc7\u7c7b\u578b\u5bf9\u8c61\u53ef\u4ee5\u8bbf\u95ee\u4efb\u610f\u503c\u7684\u7c7b\u578b\u4fe1\u606f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u4f8b\u5b50\u8868\u793a\u83b7\u53d6\u7c7b\u578b\u5bf9\u8c61\u7684\u8fc7\u7a0b\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"253\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/15-3.png\" alt=\"\" class=\"wp-image-7067\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/15-3.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/15-3-300x107.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/15-3-600x214.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"707\" height=\"94\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/16-3.png\" alt=\"\" class=\"wp-image-7069\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/16-3.png 707w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/16-3-300x40.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/16-3-600x80.png 600w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>\u7b2c10\u884c\uff1a\u901a\u8fc7reflect.TypeOf()\u53d6\u5f97\u53d8\u91cfarr1\u7684\u7c7b\u578b\u5bf9\u8c61<\/li><li>\u7b2c11\u884c\uff1a\u901a\u8fc7typeOfArr1\u7c7b\u578b\u5bf9\u8c61\u7684\u6210\u5458\u51fd\u6570\uff0c\u83b7\u53d6\u53d8\u91cf\u7684\u7c7b\u578b\u540d\u548c\u79cd\u7c7b(Kind)<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">1<strong>\u3001\u7406\u89e3\u53cd\u5c04\u7684\u7c7b\u578b(Type)\u4e0e\u79cd\u7c7b(Kind )<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4f7f\u7528\u53cd\u5c04\u65f6\uff0c \u9700\u8981\u9996\u5148\u7406\u89e3\u7c7b\u578b\uff08 Type \uff09\u548c\u79cd\u7c7b\uff08 Kind \uff09\u7684\u533a\u522b\uff0c\u7f16\u7a0b\u4e2d\uff0c\u4f7f\u7528\u6700\u591a\u7684\u662f\u7c7b\u578b\uff0c\u5728\u53cd\u5c04\u4e2d\uff0c \u5f53\u9700\u8981\u533a\u5206\u4e00\u4e2a\u5927\u54c1\u79cd\u7684\u7c7b\u578b\u65f6\uff0c\u5c31\u4f1a\u7528\u5230\u79cd\u7c7b(Kind), \u4f8b\u5982\uff0c\u9700\u8981\u7edf\u4e00\u5224\u65ad\u7c7b\u578b\u4e2d\u7684\u6307\u9488\u65f6\uff0c\u4f7f\u7528\u79cd\u7c7b\uff08 Kind \uff09\u4fe1\u606f\u5c31\u8f83\u4e3a\u65b9\u4fbf\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u7a0b\u5e8f\u4e2d\u7684\u7c7b\u578b\uff08 Type \uff09\u6307\u7684\u662f\u7cfb\u7edf\u539f\u751f\u6570\u636e\u7c7b\u578b\uff0c\u5982int \u3001string \u3001bool\u3001float32 \u7b49\uff0c\u4e5f\u5305\u62ec\u4f7f\u7528type\u5173\u952e\u5b57\u81ea\u5b9a\u4e49\u7684\u7c7b\u578b\uff0c\u8fd9\u4e9b\u7c7b\u578b\u7684\u540d\u79f0\u5c31\u662f\u5176\u7c7b\u578b\u672c\u8eab\u7684\u540d\u79f0\uff0c\u4f8b\u5982\u4f7f\u7528type A struct{}\u5b9a\u4e49\u7ed3\u6784\u4f53\u65f6\uff0cA\u5c31\u662fstruct{]\u7684\u7c7b\u578b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u79cd\u7c7b\uff08 Kind \uff09\u6307\u7684\u662f\u5bf9\u8c61\u5f52\u5c5e\u7684\u54c1\u79cd\uff0c\u5728reflect \u5305\u4e2d\u6709\u5982\u4e0b\u5b9a\u4e49\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">\u7c7b\u578b<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u79cd\u7c7b<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Invalid Kind = iota<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u975e\u6cd5\u7c7b\u578b<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Bool<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u5e03\u5c14\u578b<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Int<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u6709\u7b26\u53f7\u6574\u578b<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Int8<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u6709\u7b26\u53f78 \u4f4d\u6574\u578b<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\"> Int16<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u6709\u7b26\u53f716 \u4f4d\u6574\u578b<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\"> Int32<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u6709\u7b26\u53f732 \u4f4d\u76d1\u578b<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\"> Int64<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u6709\u7b26\u53f764 \u4f4d\u6574\u578b<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Uint<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u65e0\u7b26\u53f7\u6574\u578b<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Uint8<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u65e0\u7b26\u53f78 \u4f4d\u6574\u578b<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Uint16<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u65e0\u7b26\u53f716 \u4f4d\u6574\u578b<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Uint32<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u65e0\u7b26\u53f732 \u4f4d\u6574\u578b<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Uint64<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u65e0\u7b26\u53f764 \u4f4d\u6574\u578b<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Uintptr<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u6307\u9488<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Float32<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u5355\u7cbe\u5ea6\u6d6e\u70b9\u6570<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Float64<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u6570<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Complex64<\/td><td class=\"has-text-align-center\" data-align=\"center\">64 \u4f4d\u590d\u6570\u7c7b\u578b<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Complex128<\/td><td class=\"has-text-align-center\" data-align=\"center\">128 \u4f4d\u590d\u6570\u7c7b\u578b<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Array<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u6570\u7ec4<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Chan<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u901a\u9053<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Func<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u51fd\u6570<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Interface<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u63a5\u53e3<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Map<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u6620\u5c04<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Ptr<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u6307\u9488<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Slice<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u5207\u7247<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">String<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u5b57\u7b26\u4e32<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Struct<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u7ed3\u6784\u4f53<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Unsafe Pointer<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u5e95\u5c42\u6307\u9488<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u4f8b\u5b50\u5b9a\u4e49\u5b57\u7b26\u4e32\u7c7b\u578b\u53d8\u91cf\uff0c\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684\u5e38\u91cf\u548c\u4e00\u4e2a\u7ed3\u6784\u4f53\u7c7b\u578b\u53d8\u91cf\uff0c\u5206\u522b\u6253\u5370\u7c7b\u578b\u540d\u548c\u79cd\u7c7b\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"472\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/17-3.png\" alt=\"\" class=\"wp-image-7075\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/17-3.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/17-3-300x200.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/17-3-600x399.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"98\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/18-3.png\" alt=\"\" class=\"wp-image-7077\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/18-3.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/18-3-300x41.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/18-3-600x83.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u4e0a\u56fe\u53ef\u4ee5\u770b\u51fa\uff0c\u901a\u8fc7type\u5b9a\u4e49\u7684\u7c7b\u578b\uff0c\u7c7b\u578b\u540d\u4e3a\u81ea\u5b9a\u4e49\u7684\u503c\uff0c\u79cd\u7c7b\u4e3a\u5b9e\u9645\u5f52\u5c5e\u7684\u6570\u636e\u7c7b\u578b\uff0c\u975etype\u5b9a\u4e49\u7684\uff0c\u7c7b\u578b\u540d\u548c\u79cd\u7c7b\u540d\u76f8\u540c\uff0c\u90fd\u662f\u5b9e\u9645\u5f52\u5c5e\u7684\u6570\u636e\u7c7b\u578b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001<strong>\u6307\u9488\u4e0e\u6307\u9488\u6307\u5411\u7684\u5143\u7d20<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u7a0b\u5e8f\u4e2d\u5bf9\u6307\u9488\u83b7\u53d6\u53cd\u5c04\u5bf9\u8c61\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7reflect.Elem()\u65b9\u6cd5\u83b7\u53d6\u8fd9\u4e2a\u6307\u9488\u6307\u5411\u7684\u5143\u7d20\u7c7b\u578b\u3002\u8fd9\u4e2a\u83b7\u53d6\u8fc7\u7a0b\u88ab\u79f0\u4e3a\u53d6\u5143\u7d20\uff0c\u7b49\u6548\u4e8e\u5bf9\u6307\u9488\u7c7b\u578b\u53d8\u91cf\u505a\u4e86\u4e00\u4e2a\u201c * \u201d \u64cd\u4f5c\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"707\" height=\"296\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/19-3.png\" alt=\"\" class=\"wp-image-7088\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/19-3.png 707w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/19-3-300x126.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/19-3-600x251.png 600w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"706\" height=\"94\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/20-3.png\" alt=\"\" class=\"wp-image-7090\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/20-3.png 706w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/20-3-300x40.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/20-3-600x80.png 600w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>\u7b2c10\u884c\uff1ains\u4e3a*cat\u7c7b\u578b\u7684\u6307\u9488\u53d8\u91cf<\/li><li>\u7b2c11\u884c\uff1a\u5bf9\u6307\u9488\u53d8\u91cf\u83b7\u53d6\u53cd\u5c04\u7c7b\u578b\u4fe1\u606f<\/li><li>\u7b2c12\u884c\uff1a\u8f93\u51fa\u7c7b\u578b\u540d\u548c\u79cd\u7c7b\uff0cGo\u8bed\u8a00\u53cd\u5c04\u4e2d\u7684\u6240\u6709\u6307\u9488\u53d8\u91cf\u79cd\u7c7b\u90fd\u662fPtr\uff0c\u6307\u9488\u53d8\u91cf\u7c7b\u578b\u540d\u4e3a\u7a7a\uff0c\u4e0d\u662f*cat<\/li><li>\u7b2c13\u884c\uff1a\u53d6\u6307\u9488\u7c7b\u578b\u7684\u5143\u7d20\u7c7b\u578b\uff0c\u4e5f\u5c31\u662fcat\u7c7b\u578b\uff0c\u64cd\u4f5c\u4e0d\u53ef\u4ee5\uff0c\u4e0d\u53ef\u4ee5\u901a\u8fc7\u975e\u6307\u9488\u7c7b\u578b\u83b7\u53d6\u6307\u9488\u7c7b\u578b<\/li><li>\u7b2c14\u884c\uff1a\u518d\u6b21\u8f93\u51fa\u7ed3\u679c<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>3\u3001\u4f7f\u7528\u53cd\u5c04\u83b7\u53d6\u7ed3\u6784\u4f53\u7684\u6210\u5458\u7c7b\u578b<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4efb\u610f\u503c\u901a\u8fc7reflect. TypeOf()\u83b7\u5f97\u53cd\u5c04\u5bf9\u8c61\u4fe1\u606f\u540e\uff0c\u5982\u679c\u5b83\u7684\u7c7b\u578b\u662f\u7ed3\u6784\u4f53\uff0c\u53ef\u4ee5\u901a\u8fc7\u53cd\u5c04\u503c\u5bf9\u8c61\uff08 reflect. Type \uff09\u7684NumField()\u548cField()\u65b9\u6cd5\u83b7\u5f97\u7ed3\u6784\u4f53\u6210\u5458\u7684\u8be6\u7ec6\u4fe1\u606f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u4f8b\u5b50\u4e2d\uff0c\u5b9e\u4f8b\u5316\u4e00\u4e2a\u7ed3\u6784\u4f53\u5e76\u904d\u5386\u5176\u7ed3\u6784\u4f53\u6210\u5458\uff0c\u518d\u901a\u8fc7reflect.Type\u7684FieldByName()\u65b9\u6cd5\u67e5\u627e\u7ed3\u6784\u4f53\u4e2d\u7684\u6307\u5b9a\u540d\u79f0\u7684\u5b57\u6bb5\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"446\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/1-6.png\" alt=\"\" class=\"wp-image-7097\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/1-6.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/1-6-300x189.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/1-6-600x378.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"707\" height=\"114\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/2-6.png\" alt=\"\" class=\"wp-image-7100\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/2-6.png 707w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/2-6-300x48.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/2-6-600x97.png 600w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>\u7b2c9\u884c\uff1a\u58f0\u660e\u5e26\u67092\u4e2a\u6210\u5458\u7684cat\u7ed3\u6784\u4f53<\/li><li>\u7b2c11\u884c\uff1a\u5b57\u6bb5\u7c7b\u578b\u540e\u9762\u7528\u53cd\u5f15\u53f7&#8220;\u5f00\u59cb\u548c\u7ed3\u5c3e\u7684\u5b57\u7b26\u4e32\u88ab\u79f0\u4e3aTag(\u6807\u7b7e),\u4e00\u822c\u7528\u4e8e\u7ed9\u5b57\u6bb5\u6dfb\u52a0\u81ea\u5b9a\u4e49\u4fe1\u606f\uff0c \u65b9\u4fbf\u5176\u4ed6\u6a21\u5757\u6839\u636e\u4fe1\u606f\u8fdb\u884c\u4e0d\u540c\u529f\u80fd\u7684\u5904\u7406<\/li><li>\u7b2c13\u884c\uff1a\u521b\u5efa\u7ed3\u6784\u4f53\u5b9e\u4f8b<\/li><li>\u7b2c14\u884c\uff1a\u83b7\u53d6\u7ed3\u6784\u4f53\u53cd\u5c04\u7c7b\u578b\u5bf9\u8c61<\/li><li>\u7b2c15\u884c\uff1a\u4f7f\u7528reflect.Type \u7c7b\u578b\u7684NumField()\u65b9\u6cd5\u83b7\u5f97\u4e00\u4e2a\u7ed3\u6784\u4f53\u7c7b\u578b\u5171\u6709\u591a\u5c11\u4e2a\u5b57\u6bb5\uff0c\u5982\u679c\u7c7b\u578b\u4e0d\u662f\u7ed3\u6784\u4f53\uff0c \u5c06\u4f1a\u89e6\u53d1\u5ca9\u673a<\/li><li>\u7b2c16\u884c\uff1areflect. Type \u4e2d\u7684Field()\u65b9\u6cd5\u548cNurnField()\u4e00\u822c\u90fd\u662f\u914d\u5bf9\u4f7f\u7528\uff0c\u7528\u6765\u5b9e\u73b0\u7ed3\u6784\u4f53\u6210\u5458\u7684\u904d\u5386\u64cd\u4f5c<\/li><li>\u7b2c17\u884c\uff1a\u4f7f\u7528reflect.Type \u7684Field()\u65b9\u6cd5\u8fd4\u56de\u7684\u7ed3\u6784\u4e0d\u518d\u662freflect.Type \u800c\u662fStructField \u7ed3\u6784\u4f53,Name\u548cTag\u90fd\u662f\u5176\u4e2d\u7684\u6210\u5458\u53d8\u91cf<\/li><li>\u7b2c19\u884c\uff1a\u4f7f\u7528reflect.Type \u7684FieldByName()\u6839\u636e\u5b57\u6bb5\u540d\u67e5\u627e\u7ed3\u6784\u4f53\u5b57\u6bb5\u4fe1\u606f\uff0ccatType\u8868\u793a\u8fd4\u56de\u7684\u7ed3\u6784\u4f53\u5b57\u6bb5\u4fe1\u606f\uff0c \u7c7b\u578b\u4e3aStructField, ok \u8868\u793a\u662f\u5426\u627e\u5230\u7ed3\u6784\u4f53\u5b57\u6bb5\u7684\u4fe1\u606f<\/li><li>\u7b2c20\u884c\uff1a\u4f7f\u7528StructField \u4e2dTag \u7684Get()\u65b9\u6cd5\uff0c\u6839\u636eTag \u4e2d\u7684\u540d\u5b57\u8fdb\u884c\u4fe1\u606f\u83b7\u53d6<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">reflect.Type \u7684Field()\u65b9\u6cd5\u8fd4\u56deStructField \u7ed3\u6784\uff0c\u8fd9\u4e2a\u7ed3\u6784\u63cf\u8ff0\u7ed3\u6784\u4f53\u7684\u6210\u5458\u4fe1\u606f\uff0c\u901a\u8fc7\u8fd9\u4e2a\u4fe1\u606f\u53ef\u4ee5\u83b7\u53d6\u6210\u5458\u4e0e\u7ed3\u6784\u4f53\u7684\u5173\u7cfb\uff0c\u5982\u504f\u79fb\u3001\u7d22\u5f15\u3001\u662f\u5426\u4e3a\u533f\u540d\u5b57\u6bb5\u3001\u7ed3\u6784\u4f53\u6807\u7b7e(Tag)\u7b49\uff0c\uff0c\u800c\u4e14\u8fd8\u53ef\u4ee5\u901a\u8fc7StructField \u7684Type \u5b87\u6bb5\u8fdb\u4e00\u6b65\u83b7\u53d6\u7ed3\u6784\u4f53\u6210\u5458\u7684\u7c7b\u578b\u4fe1\uff0cStructField \u7684\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type StructField struct {\n       Name string        \/\/\u5b57\u6bb5\u540d         \n       PkgPath string     \/\/\u5b57\u6bb5\u8def\u5f84   \n       Type Type          \/\/\u5b57\u6bb5\u53cd\u5c04\u7c7b\u578b\u5bf9\u8c61\n       Tag StructTag      \/\/\u5b57\u6bb5\u7684\u7ed3\u6784\u4f53\u6807\u7b7e\n       Offset uintptr     \/\/\u5b57\u6bb5\u5728\u7ed3\u6784\u4f53\u4e2d\u7684\u76f8\u5bf9\u504f\u79fb\n       Index &#91;]int        \/\/Type.FieldByindex \u4e2d\u7684\u8fd4\u56de\u7684\u7d22\u5f15\u503c\n       Anonymous bool     \/\/\u662f\u5426\u4e3a\u533f\u540d\u5b57\u6bb5\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">\u65b9\u6cd5<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u8bf4\u660e<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Field(i int) StructField<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u6839\u636e\u7d22\u5f15\u8fd4\u56de\u7d22\u5f15\u5bf9\u5e94\u7684\u5b57\u6bb5\u4fe1\u606f\uff0c\u8fd4\u56de\u7684\u662fStructField\u7ed3\u6784\u4f53<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">NumField() int<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u8fd4\u56de\u7ed3\u6784\u4f53\u6210\u5458\u5b57\u6bb5\u6570\u91cf\uff0c\u8fd4\u56de\u503c\u4e3a\u6574\u578b<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">FieldByName(name string) (StructField,bool)<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u6839\u636e\u7ed9\u5b9a\u5b57\u7b26\u4e32\u8fd4\u56de\u5b57\u7b26\u4e32\u5bf9\u5e94\u7684\u7ed3\u6784\u4f53\u5b57\u6bb5\u7684\u4fe1\u606f,\u6ca1\u6709\u627e\u5230\u65f6bool\u8fd4\u56defalse<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">FieldBylndex(index []int) StructField<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u591a\u5c42\u6210\u5458\u8bbf\u95ee\u65f6\uff0c\u6839\u636e[]int\u63d0\u4f9b\u7684\u6bcf\u4e2a\u7ed3\u6784\u4f53\u7684\u5b57\u6bb5\u7d22\u5f15\uff0c\u8fd4\u56de\u5b57\u6bb5\u7684\u4fe1\u606f,\u6ca1\u6709\u627e\u5230\u65f6\u8fd4\u56de\u96f6\u503c<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">FieldByNameFunc( match func(string)bool) (StructField,bool)<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u6839\u636e\u5339\u914d\u51fd\u6570\u5339\u914d\u9700\u8981\u7684\u5b57\u6bb5<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u4e0a\u8ff0\u8868\u683c\u4e2d\uff0c\u5f53\u7c7b\u578b(\u6216\u503c)\u4e0d\u662f\u7ed3\u6784\u4f53\u6216\u7d22\u5f15\u8d85\u754c\u65f6\u5c06\u53d1\u751f\u5b95\u673a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>4\u3001\u7ed3\u6784\u4f53\u6807\u7b7e(Struct Tag)&#8212;\u5bf9\u7ed3\u6784\u4f53\u5b57\u6bb5\u7684\u989d\u5916\u4fe1\u606f\u6807\u7b7e<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7reflect.Type \u83b7\u53d6\u7ed3\u6784\u4f53\u6210\u5458\u4fe1\u606freflect.StructField \u7ed3\u6784\u4e2d\u7684Tag \u88ab\u79f0\u4e3a\u7ed3\u6784\u4f53\u6807\u7b7e\uff08 Struct Tag \uff09<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">JSON \u3001BSON \u7b49\u683c\u5f0f\u8fdb\u884c\u5e8f\u5217\u5316\u53ca\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04(Object Relational Mapping \uff0c \u7b80\u79f0ORM \uff09\u7cfb\u7edf\u90fd\u4f1a\u7528\u5230\u7ed3\u6784\u4f53\u6807\u7b7e\uff0c\u8fd9\u4e9b\u7cfb\u7edf\u4f7f\u7528\u6807\u7b7e\u8bbe\u5b9a\u5b57\u6bb5\u5728\u5904\u7406\u65f6\u5e94\u8be5\u5177\u5907\u7684\u7279\u6b8a\u5c5e\u6027\u548c\u53ef\u80fd\u53d1\u751f\u7684\u884c\u4e3a\u3002\u8fd9\u4e9b\u4fe1\u606f\u90fd\u662f\u9759\u6001\u7684\uff0c\u65e0\u987b\u5b9e\u4f8b\u5316\u7ed3\u6784\u4f53\uff0c \u53ef\u4ee5\u901a\u8fc7\u53cd\u5c04\u83b7\u53d6\u5230<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Tag \u5728\u7ed3\u6784\u4f53\u5b57\u6bb5\u540e\u65b9\u4e66\u5199\u7684\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>`keyl:\"valuel\" key2:\"value2\"`             \/\/\u7528\u53cd\u5f15\u53f7\u62ec\u8d77\u6765<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ed3\u6784\u4f53\u6807\u7b7e\u7531\u4e00\u4e2a\u6216\u591a\u4e2a\u952e\u503c\u5bf9\u7ec4\u6210\u3002\u952e\u4e0e\u503c\u4f7f\u7528\u5192\u53f7\u5206\u9694\uff0c\u503c\u7528\u53cc\u5f15\u53f7\u62ec\u8d77\u6765\u3002\u952e\u4e0e\u503c\u4e4b\u95f4\u6ca1\u6709\u7a7a\u683c\uff0c\u952e\u503c\u5bf9\u4e4b\u95f4\u4f7f\u7528\u4e00\u4e2a\u7a7a\u683c\u5206\u9694<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">StructTag \u62e5\u6709\u4e00\u4e9b\u65b9\u6cd5\uff0c \u53ef\u4ee5\u8fdb\u884cTag \u4fe1\u606f\u7684\u89e3\u6790\u548c\u63d0\u53d6\uff0c\u5982\u4e0b\u6240\u793a:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>func(tag StructTag) Get(key string) string \uff1a \u6839\u636eTag \u4e2d\u7684\u952e\u83b7\u53d6\u5bf9\u5e94\u7684\u503c\uff0c\u4f8b\u5982`key1:&#8221;value1&#8243;  key2:&#8221;value2&#8243;` \u7684Tag \u4e2d\uff0c \u53ef\u4ee5\u4f20\u5165\u201c key1\u201d\u83b7\u5f97\u201cvalue1&#8243;<\/li><li>func (tag StructTag) Lookup(key string) (value string, ok bool\uff09\uff1a\u6839\u636eTag \u4e2d\u7684\u952e\uff0c \u67e5\u8be2\u503c\u662f\u5426\u5b58\u5728<\/li><\/ul>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\"><strong>\u4e8c\u3001\u53cd\u5c04\u7684\u503c\u5bf9\u8c61<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53cd\u5c04\u4e0d\u4ec5\u53ef\u4ee5\u83b7\u53d6\u503c\u7684\u7c7b\u578b\u4fe1\u606f\uff0c\u8fd8\u53ef\u4ee5\u52a8\u6001\u5730\u83b7\u53d6\u6216\u8005\u8bbe\u7f6e\u53d8\u91cf\u7684\u503c\uff0cGo \u8bed\u8a00\u4e2d\u4f7f\u7528reflect.Value \u83b7\u53d6\u548c\u8bbe\u7f6e\u53d8\u91cf\u7684\u503c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>1\u3001\u4f7f\u7528\u53cd\u5c04\u503c\u5bf9\u8c61\u5305\u88c5\u4efb\u610f\u503c<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u8bed\u8a00\u4e2d\uff0c \u4f7f\u7528reflect.ValueOf()\u51fd\u6570\u83b7\u5f97\u503c\u7684\u53cd\u5c04\u503c\u5bf9\u8c61\uff08 reflect. Value \uff09\uff0c\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>value := reflect.ValueOf(rawValue)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">reflect.ValueOf \u8fd4\u56dereflect.Value \u7c7b\u578b\uff0c\u5305\u542b\u6709rawValue \u7684\u503c\u4fe1\u606f\u3002reflect.Value \u4e0e\u539f\u503c\u95f4\u53ef\u4ee5\u901a\u8fc7\u503c\u5305\u88c5\u548c\u503c\u83b7\u53d6\u4e92\u76f8\u8f6c\u5316\uff0creflect. Value \u662f\u4e00\u4e9b\u53cd\u5c04\u64cd\u4f5c\u7684\u91cd\u8981\u7c7b\u578b\uff0c\u5982\u53cd\u5c04\u8c03\u7528\u51fd\u6570<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u4f8b\u5b50\u8868\u793a\u4ece<strong>\u53cd\u5c04\u503c\u5bf9\u8c61(reflect.Value)\u4e2d\u83b7\u53d6\u503c<\/strong>\u7684\u4f8b\u5b50\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"706\" height=\"283\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/3-6.png\" alt=\"\" class=\"wp-image-7124\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/3-6.png 706w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/3-6-300x120.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/3-6-600x241.png 600w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"102\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/4-6.png\" alt=\"\" class=\"wp-image-7125\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/4-6.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/4-6-300x43.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/4-6-600x86.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>\u7b2c9\u884c\uff1a\u58f0\u660e\u4e00\u4e2a\u53d8\u91cf\uff0c\u7c7b\u578b\u4e3aint\uff0c\u521d\u59cb\u503c\u4e3a100<\/li><li>\u7b2c10\u884c\uff1a\u83b7\u53d6\u53d8\u91cfa \u7684\u53cd\u5c04\u503c\u5bf9\u8c61\uff0c\u7c7b\u578b\u4e3areflect.Value\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u548creflect.TypeOf()\u7c7b\u4f3c<\/li><li>\u7b2c11\u884c\uff1a\u5c06valueOfA \u53cd\u5c04\u503c\u5bf9\u8c61\u4ee5interface{}\u7c7b\u578b\u53d6\u51fa\uff0c\u901a\u8fc7\u7c7b\u578b\u65ad\u8a00\u8f6c\u6362\u4e3aint\u7c7b\u578b\u5e76\u8d4b\u503c\u7ed9getA<\/li><li>\u7b2c12\u884c\uff1a\u5c06valueOfA \u53cd\u5c04\u503c\u5bf9\u8c61\u901a\u8fc7Int \u65b9\u6cd5\uff0c\u4ee5int64 \u7c7b\u578b\u53d6\u51fa\uff0c\u901a\u8fc7\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\uff0c\u8f6c\u6362\u4e3a\u539f\u672c\u7684int\u7c7b\u578b<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u51e0\u79cd\u65b9\u6cd5\u4ece\u53cd\u5c04\u503c\u5bf9\u8c61reflect.Value \u4e2d\u83b7\u53d6\u539f\u503c\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">\u65b9\u6cd5\u540d<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u8bf4\u660e<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Interface() interface{}<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u5c06\u503c\u4ee5interface{}\u7c7b\u578b\u8fd4\u56de\uff0c\u53ef\u4ee5\u901a\u8fc7\u7c7b\u578b\u65ad\u8a00\u8f6c\u6362\u4e3a\u6307\u5b9a\u7c7b\u578b<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Int() int64<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u5c06\u503c\u4ee5int\u7c7b\u578b\u8fd4\u56de\uff0c\u6240\u6709\u6709\u7b26\u53f7\u6574\u578b\u5747\u53ef\u4ee5\u6b64\u65b9\u5f0f\u8fd4\u56de<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Uint() uint64<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u5c06\u503c\u4ee5uint \u7c7b\u578b\u8fd4\u56de\uff0c\u6240\u6709\u65e0\u7b26\u53f7\u6574\u578b\u5747\u53ef\u4ee5\u6b64\u65b9\u5f0f\u8fd4\u56de<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Float() float64<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u5c06\u503c\u4ee5\u53cc\u7cbe\u5ea6(float64) \u7c7b\u578b\u8fd4\u56de\uff0c\u6240\u6709\u6d6e\u70b9\u6570(float32 \u3001float64 )\u5747\u53ef\u4ee5\u6b64\u65b9\u5f0f\u8fd4\u56de<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Bool() bool<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u5c06\u503c\u4ee5bool \u7c7b\u578b\u8fd4\u56de<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Bytes() []bytes<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u5c06\u503c\u4ee5\u5b57\u8282\u6570\u7ec4[]bytes\u7c7b\u578b\u8fd4\u56de<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">String() string<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u5c06\u503c\u4ee5\u5b57\u7b26\u4e32\u7c7b\u578b\u8fd4\u56de<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">2<strong>\u3001\u4f7f\u7528\u53cd\u5c04\u8bbf\u95ee\u7ed3\u6784\u4f53\u7684\u6210\u5458\u5b57\u6bb5\u7684\u503c<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53cd\u5c04\u503c\u5bf9\u8c61(reflect. Value \uff09\u63d0\u4f9b\u5bf9\u7ed3\u6784\u4f53\u8bbf\u95ee\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5b8c\u6210\u5bf9\u7ed3\u6784\u4f53\u4efb\u610f\u503c\u7684\u8bbf\u95ee<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">\u65b9\u6cd5<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u5907\u6ce8<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Field(i int) Value<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u6839\u636e\u7d22\u5f15\uff0c\u8fd4\u56de\u7d22\u5f15\u5bf9\u5e94\u7684\u7ed3\u6784\u4f53\u6210\u5458\u5b57\u6bb5\u7684\u53cd\u5c04\u503c\u5bf9\u8c61<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">NumField() int<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u8fd4\u56de\u7ed3\u6784\u4f53\u6210\u5458\u5b57\u6bb5\u6570\u91cf<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">FieldByName(name string) Value<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u6839\u636e\u7ed9\u5b9a\u5b57\u7b26\u4e32\u8fd4\u56de\u5b57\u7b26\u4e32\u5bf9\u5e94\u7684\u7ed3\u6784\u4f53\u5b57\u6bb5\u3002\u6ca1\u6709\u627e\u5230\u65f6\u8fd4\u56de\u96f6\u503c<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">FieldBylndex(index []int) Value<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u591a\u5c42\u6210\u5458\u8bbf\u95ee\u65f6\uff0c\u6839\u636e[]int\u63d0\u4f9b\u7684\u6bcf\u4e2a\u7ed3\u6784\u4f53\u7684\u5b57\u6bb5\u7d22\u5f15\uff0c\u8fd4\u56de\u5b57\u6bb5\u7684\u503c\u3002\u6ca1\u6709\u627e\u5230\u65f6\u8fd4\u56de\u96f6\u503c<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">FieldByNameFunc( match func(string) bool) Value<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u6839\u636e\u5339\u914d\u51fd\u6570\u5339\u914d\u9700\u8981\u7684\u5b57\u6bb5\u3002\u627e\u5230\u65f6\u8fd4\u56de\u96f6\u503c<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u4e0a\u9762\u8868\u683c\u4e2d  \u5f53\u503c\u4e0d\u662f\u7ed3\u6784\u4f53\u6216\u7d22\u5f15\u8d85\u754c\u65f6\u53d1\u751f\u5ca9\u673a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">(1)\u3001\u4e0b\u9762\u4f8b\u5b50\u6784\u9020\u4e86\u4e00\u4e2a\u7ed3\u6784\u4f53\u5305\u542b\u4e0d\u540c\u7c7b\u578b\u7684\u6210\u5458\uff0c\u901a\u8fc7reflect.Value \u63d0\u4f9b\u7684\u6210\u5458\u8bbf\u95ee\u51fd\u6570\uff0c\u53ef\u4ee5\u83b7\u5f97\u7ed3\u6784\u4f53\u503c\u7684\u5404\u79cd\u6570\u636e\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"707\" height=\"447\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/5-6.png\" alt=\"\" class=\"wp-image-7138\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/5-6.png 707w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/5-6-300x190.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/5-6-600x379.png 600w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"707\" height=\"136\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/6-6.png\" alt=\"\" class=\"wp-image-7140\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/6-6.png 707w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/6-6-300x58.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/6-6-600x115.png 600w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>\u7b2c8-14\u884c\uff1a\u5b9a\u4e49\u7ed3\u6784\u4f53\uff0c\u6bcf\u4e2a\u5b57\u6bb5\u7c7b\u578b\u90fd\u4e0d\u4e00\u6837<\/li><li>\u7b2c17\u884c\uff1a\u5b9e\u4f8b\u5316\u7ed3\u6784\u4f53\u5e76\u5305\u88c5\u4e3areflect.Value \u7c7b\u578b\uff0c\u6210\u5458\u4e2d\u5305\u542b\u4e00\u4e2a*dummy \u7684\u5b9e\u4f8b<\/li><li>\u7b2c18\u884c\uff1a\u83b7\u53d6\u7ed3\u6784\u4f53\u7684\u5b57\u6bb5\u6570\u91cf<\/li><li>\u7b2c20\u884c\uff1a\u83b7\u53d6\u7d22\u5f15\u4e3a2 \u7684\u5b57\u6bb5\u503c\uff08 float32 \u5b57\u6bb5\u3009\uff0c \u5e76\u4e14\u6253\u5370\u7c7b\u578b<\/li><li>\u7b2c21\u884c\uff1a\u6839\u636e\u201c b \u201d\u5b57\u7b26\u4e32\uff0c\u67e5\u627e\u5230b \u5b87\u6bb5\u7684\u7c7b\u578b<\/li><li>\u7b2c22\u884c\uff1a\uff3b]int{4,0}\u4e2d\u76844 \u8868\u793a\uff0c\u5728dummy \u7ed3\u6784\u4e2d\u7d22\u5f15\u503c\u4e3a4 \u7684\u6210\u5458\uff0c\u4e5f\u5c31\u662fc\uff0cc\u7684\u7684\u7c7b\u578b\u4e3adummy \uff0c\u4e5f\u662f\u4e00\u4e2a\u7ed3\u6784\u4f53\uff0c\u56e0\u6b64\u4f7f\u7528[]int {4,0 \uff5d\u4e2d\u76840\u7ee7\u7eed\u5728c\u503c\u7684\u57fa\u7840\u4e0a\u7d22\u5f15\uff0c \u7ed3\u6784\u4e3adummy \u4e2d\u7d22\u5f15\u503c\u4e3a0 \u7684a \u5b57\u6bb5\uff0c \u7c7b\u578b\u4e3aint<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">(2)\u3001\u4e0b\u9762\u4f8b\u5b50\u4e2d\uff0c\u5c06\u7ed3\u6784\u4f53\u5207\u7247b\u4f20\u9012\u7ed9\u51fd\u6570gong()\uff0c\u51fd\u6570\u53c2\u6570\u4e3a\u7a7a\u63a5\u53e3\u7c7b\u578b\uff0c\u53ef\u63a5\u53d7\u4efb\u610f\u7c7b\u578b\u53c2\u6570\uff0c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\r\n\r\nimport (\r\n\t\"fmt\"\r\n\t\"reflect\"\r\n)\r\n\r\ntype def struct {\r\n\tName string\r\n\tPass string\r\n}\r\ntype s struct {\r\n\tName string\r\n\tPass string\r\n}\r\n\r\n\/\/\u63a5\u6536\u4efb\u610f\u53c2\u6570,\u6700\u7ec8\u8fd4\u56de\u5207\u7247\u7c7b\u578b\r\nfunc gong(bb interface{}) &#91;]s {\n\r        \/\/\u8c03\u7528\u51fd\u6570ifSlice\u5224\u65ad\u4f20\u5165\u7684\u53c2\u6570\u662f\u4e0d\u662f\u4e00\u4e2a\u5207\u7247\n\tval, ok := isSlice(bb)\r\n\tif !ok {\r\n\t\treturn nil\r\n\t}\r\r\n\t\/\/\u901a\u8fc7val.Len()\u83b7\u53d6\u53cd\u5c04\u503c\u5bf9\u8c61\u7684\u957f\u5ea6\r\n\tsliceLen := val.Len()\r\n\t\/\/\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7ed3\u6784\u4f53\u5207\u7247,s\u4e3a\u4e0a\u9762\u5b9a\u4e49\u7684\u7ed3\u6784\u4f53,\u957f\u5ea6\u4e3asliceLen\r\n\tout := make(&#91;]s, sliceLen)\r\n\tfor i := 0; i &lt; sliceLen; i++ {\r\n\t\t\/\/\u901a\u8fc7for\u5faa\u73af\u83b7\u53d6val\u503c\u7684\u7b2ci\u4e2a\u5143\u7d20\r\n\t\tvalue := val.Index(i)\n\r                \/\/\u83b7\u53d6value\u7684\u7c7b\u578b\n\t\ttype1 := value.Type()\n\r                \/\/\u5224\u65ad\u6b64\u79cd\u7c7b\u662f\u4e0d\u662f\u7ed3\u6784\u4f53\n\t\tif type1.Kind() != reflect.Struct {\r\n\t\t\tpanic(\"Kind is not struct\")\r\n\t\t}\n\r                \/\/\u5982\u679c\u662f\u7ed3\u6784\u4f53,\u8c03\u7528FieldByName\u6839\u636e\u7ed9\u5b9a\u5b57\u7b26\u4e32\u83b7\u53d6\u5b57\u6bb5\u5bf9\u5e94\u4fe1\u606f\n\t\tout&#91;i].Name = value.FieldByName(\"Name\").String()\r\n\t\tout&#91;i].Pass = value.FieldByName(\"Pass\").String()\r\n\t}\r\n\treturn out\r\n}\r\n\r\n\/\/\u5224\u65ad\u662f\u5426\u4e3a\u5207\u7247,\u901a\u8fc7\u53cd\u5c04\r\nfunc isSlice(arg interface{}) (val reflect.Value, ok bool) {\r\n\t\/\/\u4f7f\u7528reflect.ValudOf\u83b7\u53d6\u53cd\u5c04\u503c\u5bf9\u8c61reflect.Value\r\n\tval = reflect.ValueOf(arg)\r\n\t\/\/\u5224\u65adval\u7684\u79cd\u7c7b\u662f\u4e0d\u662f\u5207\u7247\r\n\tif val.Kind() == reflect.Slice {\r\n\t\tok = true\r\n\t}\r\n\treturn\r\n}\r\nfunc main() {\r\n\tb := &#91;]def{\r\n\t\t{Name: \"hello\", Pass: \"111\"},\r\n\t\t{Name: \"world\", Pass: \"888\"},\r\n\t}\r\n\tresult := gong(b)\r\n\tfmt.Println(\"\u7ed3\u679c\u4e3a:\",result)\r\n}\r<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>val\u867d\u7136\u662f\u79cd\u7c7b\u4e3a\u5207\u7247\uff0c\u4f46\u662f\u5176\u7c7b\u578b\u4e3a\u53cd\u5c04\u503c\u5bf9\u8c61\uff0c\u4e0d\u53ef\u904d\u5386<\/li><li>value\u7684\u79cd\u7c7b\u867d\u7136\u5df2\u7ecf\u662f\u7ed3\u6784\u4f53\uff0c\u4f46\u662f\u4e0d\u53ef\u76f4\u63a5\u904d\u5386\uff0c\u5fc5\u987b\u901a\u8fc7FieldByName<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"768\" height=\"157\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/10\/7.png\" alt=\"\" class=\"wp-image-10158\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/10\/7.png 768w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/10\/7-300x61.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/10\/7-600x123.png 600w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/figure>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u6ce8\u610f\uff1a\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\u5c06\u4e24\u4e2a\u7ed3\u6784\u4f53\u5b9a\u4e49\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u5b9e\u9645\u5e94\u7528\u7684\u7684\u4e3b\u8981\u65b9\u5f0f\u662f\u5411\u5176\u4ed6\u6587\u4ef6\u4e2d\u4f20\u9012\u7ed3\u6784\u4f53\u5207\u7247\uff0c\u5e76\u4e14\u65b0\u7684\u7ed3\u6784\u4f53s\u548c\u51fd\u6570gong\u90fd\u8981\u5b9a\u4e49\u5728\u5176\u4f59\u6587\u4ef6\u4e2d\uff0c\u8fdb\u884c\u5904\u7406\u4f20\u9012\u8fc7\u53bb\u7684\u7ed3\u6784\u4f53\u5207\u7247\u5e76\u8fd4\u56de\u5904\u7406\u4e4b\u540e\u7684\uff0c\u6bd4\u5982\u8fdb\u884c\u5b57\u6bb5\u52a0\u5bc6\u540e\u8fd4\u56de<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>3\u3001\u53cd\u5c04\u5bf9\u8c61\u7684\u7a7a\u548c\u6709\u6548\u6027\u5224\u65ad<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53cd\u5c04\u503c\u5bf9\u8c61\uff08 reflect.Value \uff09\u63d0\u4f9b\u4e00\u7cfb\u5217\u65b9\u6cd5\u8fdb\u884c\u96f6\u503c\u548c\u7a7a\u5224\u5b9a\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">\u65b9\u6cd5<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u8bf4\u660e<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">IsNil() bool<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u8fd4\u56de\u503c\u662f\u5426\u4e3anil \u3002\u5982\u679c\u503c\u7c7b\u578b\u4e0d\u662f\u901a\u9053(channel) \u3001\u51fd\u6570\u3001\u63a5\u53e3\u3001map \u3001\u6307\u9488\u6216\u5207\u7247\u65f6\u53d1\u751fpanic<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">IsValid() bool<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u5224\u65ad\u503c\u662f\u5426\u6709\u6548\uff0c\u5f53\u503c\u672c\u8eab\u975e\u6cd5\u65f6\uff0c\u8fd4\u56defalse \uff0c\u4f8b\u5982reflect Value\u4e0d\u5305\u542b\u4efb\u4f55\u503c\uff0c\u503c\u4e3anil \u7b49<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u4f8b\u5b50\u5bf9\u5404\u79cd\u65b9\u5f0f\u7684\u7a7a\u6307\u9488\u8fdb\u884cIsNil \u548cIsValid \u7684\u8fd4\u56de\u503c\u5224\u5b9a\u68c0\u6d4b\uff0c\u540c\u65f6\u5bf9\u7ed3\u6784\u4f53\u6210\u5458\u53ca\u65b9\u6cd5\u67e5\u627emap \u952e\u503c\u5bf9\u7684\u8fd4\u56de\u503c\u8fdb\u884cIsValid \u5224\u5b9a\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"707\" height=\"363\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/7-6.png\" alt=\"\" class=\"wp-image-7148\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/7-6.png 707w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/7-6-300x154.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/7-6-600x308.png 600w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"166\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/8-6.png\" alt=\"\" class=\"wp-image-7150\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/8-6.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/8-6-300x70.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/8-6-600x141.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<ul class=\"has-white-background-color has-background wp-block-list\"><li>\u7b2c9\u884c\uff1a\u58f0\u660e\u4e00\u4e2a*int \u7c7b\u578b\u7684\u6307\u9488\uff0c\u521d\u59cb\u503c\u4e3anil<\/li><li>\u7b2c10\u884c\uff1a\u5c06\u53d8\u91cfa \u5305\u88c5\u4e3areflect.Value \u5e76\u4e14\u5224\u65ad\u662f\u5426\u4e3a\u7a7a\uff0c\u6b64\u65f6\u53d8\u91cfa \u4e3a\u7a7a\u6307\u9488\uff0c\u56e0\u6b64\u8fd4\u56detrue<\/li><li>\u7b2c11\u884c\uff1a\u5bf9nil \u8fdb\u884cIsValid (\u6709\u6548\u6027\uff09\u5224\u5b9a\uff0c\u8fd4\u56defalse<\/li><li>\u7b2c12\u884c\uff1a(*int)(nil\uff09\u7684\u542b\u4e49\u662f\u5c06nil \u8f6c\u6362\u4e3a*int \uff0c\u4e5f\u5c31\u662f*int \u7c7b\u578b\u7684\u7a7a\u6307\u9488\uff0c\u6b64\u884c\u5c06nil \u8f6c\u6362\u4e3a*int \u7c7b\u578b\uff0c\u5e76\u53d6\u6307\u9488\u6307\u5411\u5143\u7d20,\u7531\u4e8enil \u4e0d\u6307\u5411\u4efb\u4f55\u5143\u7d20\uff0c*int \u7c7b\u578b\u7684nil\u4e5f\u4e0d\u80fd\u6307\u5411\u4efb\u4f55\u5143\u7d20\uff0c\u503c\u4e0d\u662f\u6709\u6548\u7684\u3002\u56e0\u6b64\u8fd9\u4e2a\u53cd\u5c04\u503c\u4f7f\u7528Isvalid \u5224\u65ad\u65f6\u8fd4\u56defalse<\/li><li>\u7b2c13\u884c\uff1a\u5b9e\u4f8b\u5316\u4e00\u4e2a\u7ed3\u6784\u4f53<\/li><li>\u7b2c14\u884c\uff1a\u901a\u8fc7FieldByName \u67e5\u627es \u7ed3\u6784\u4f53\u4e2d\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32\u7684\u6210\u5458\uff0c\u5982\u6210\u5458\u4e0d\u5b58\u5728\uff0cIsValid \u8fd4\u56defalse<\/li><li>\u7b2c15\u884c\uff1a\u901a\u8fc7MethodByName \u67e5\u627es \u7ed3\u6784\u4f53\u4e2d\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff0c\u5982\u65b9\u6cd5\u4e0d\u5b58\u5728\uff0cIsValid \u8fd4\u56defalse<\/li><li>\u7b2c16\u884c\uff1a\u5b9e\u4f8b\u5316\u4e00\u4e2amap \uff0c\u8fd9\u79cd\u5199\u6cd5\u4e0emake \u65b9\u5f0f\u521b\u5efa\u7684map\u7b49\u6548<\/li><li>\u7b2c17\u884c\uff1aMaplndex()\u65b9\u6cd5\u80fd\u6839\u636e\u7ed9\u5b9a\u7684reflect. Value \u7c7b\u578b\u7684\u503c\u67e5\u627emap \uff0c\u5e76\u4e14\u8fd4\u56de\u67e5\u627e\u5230\u7684\u7ed3\u679c<\/li><\/ul>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">IsNil \u5e38\u88ab\u7528\u4e8e\u5224\u65ad\u6307\u9488\u662f\u5426\u4e3a\u7a7a\uff1b IsValid \u5e38\u88ab\u7528\u4e8e\u5224\u5b9a\u8fd4\u56de\u503c\u662f\u5426\u6709\u6548<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>4\u3001\u4f7f\u7528\u53cd\u5c04\u503c\u5bf9\u8c61\u4fee\u6539\u53d8\u91cf\u7684\u503c<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528reflect.Value \u5bf9\u5305\u88c5\u7684\u503c\u8fdb\u884c\u4fee\u6539\u65f6\uff0c \u9700\u8981\u9075\u5faa\u4e00\u4e9b\u89c4\u5219\uff0c\u5982\u679c\u6ca1\u6709\u6309\u7167\u89c4\u5219\u8fdb\u884c\u4ee3\u7801\u8bbe\u8ba1\u548c\u7f16\u5199\uff0c\u8f7b\u5219\u65e0\u6cd5\u4fee\u6539\u5bf9\u8c61\u503c\uff0c\u91cd\u5219\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u4f1a\u53d1\u751f\u5ca9\u673a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528reflect.Value \u53d6\u5143\u7d20\u3001\u53d6\u5730\u5740\u53ca\u4fee\u6539\u503c\u7684\u5c5e\u6027\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">\u65b9\u6cd5\u540d<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u5907\u6ce8<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Elem() Value<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u53d6\u503c\u6307\u5411\u7684\u5143\u7d20\u503c,\u7c7b\u4f3c\u4e8e\u8bed\u8a00\u5c42&#8221;*&#8221;\u64cd\u4f5c\u3002\u5f53\u503c\u7c7b\u578b\u4e0d\u662f\u6307\u9488\u6216\u63a5\u53e3\u65f6\u53d1\u751f\u5b95\u673a,\u7a7a\u6307\u9488\u65f6\u8fd4\u56denil\u7684Value<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Addr() Value<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u5bf9\u53ef\u5bfb\u5740\u7684\u503c\u8fd4\u56de\u5176\u5730\u5740\uff0c\u7c7b\u4f3c\u4e8e\u8bed\u8a00\u5c42\u201c\uff06\u201d\u64cd\u4f5c\u3002\u5f53\u503c\u4e0d\u53ef\u5bfb\u5740\u65f6\u53d1\u751f\u5b95\u673a<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">CanAddr() bool<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u8868\u793a\u503c\u662f\u5426\u53ef\u5bfb\u5740<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">CanSet() bool<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u8fd4\u56de\u503c\u80fd\u5426\u88ab\u4fee\u6539\uff0c\u8981\u6c42\u503c\u53ef\u5bfb\u5740\u4e14\u662f\u5bfc\u51fa\u7684\u5b57\u6bb5<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528reflect.Value \u4fee\u6539\u503c\u7684\u76f8\u5173\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">Set( x Value)<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u5c06\u503c\u8bbe\u7f6e\u4e3a\u4f20\u5165\u7684\u53cd\u5c04\u503c\u5bf9\u8c61\u7684\u503c<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Setlnt(x int64)<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u4f7f\u7528int64\u8bbe\u7f6e\u503c\u3002\u5f53\u503c\u7684\u7c7b\u578b\u4e0d\u662fmt \u3001int8 \u3001intl6 \u3001int32 \u3001int64\u65f6\u4f1a\u53d1\u751f\u5b95\u673a<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">SetUint(x uint64)<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u4f7f\u7528uint64\u8bbe\u7f6e\u503c\u3002\u5f53\u503c\u7684\u7c7b\u578b\u4e0d\u662fuint\u3001uint8 \u3001uintl6 \u3001uint32 , uint64\u65f6\u4f1a\u53d1\u751f\u5b95\u673a<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">SetFloat(x float64)<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u4f7f\u7528float64\u8bbe\u7f6e\u503c\u3002\u5f53\u503c\u7684\u7c7b\u578b\u4e0d\u662ffloat32 \u3001float64\u65f6\u4f1a\u53d1\u751f\u5b95\u673a<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">SetBool(x bool)<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u4f7f\u7528booI\u8bbe\u7f6e\u503c\uff0c\u5f53\u503c\u7684\u7c7b\u578b\u4e0d\u662fbool\u65f6\u4f1a\u53d1\u751f\u5b95\u673a<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">SetBytes(x [)byte)<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u8bbe\u7f6e\u5b57\u8282\u6570\u7ec4[]bytes\u503c\u3002\u5f53\u503c\u7684\u7c7b\u578b\u4e0d\u662f[]byte\u65f6\u4f1a\u53d1\u751f\u5b95\u673a<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">SetString(x string)<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u8bbe\u7f6e\u5b57\u7b26\u4e32\u503c\u3002\u5f53\u503c\u7684\u7c7b\u578b\u4e0d\u662fstring\u65f6\u4f1a\u53d1\u751f\u5b95\u673a<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u5728reflect. Value \u7684CanSet \u8fd4\u56defalse \u4ecd\u7136\u4fee\u6539\u503c\u65f6\u4f1a\u53d1\u751f\u5b95\u673a\uff0c\u5728\u5df1\u77e5\u503c\u7684\u7c7b\u578b\u65f6\uff0c\u5e94\u5c3d\u91cf\u4f7f\u7528\u503c\u5bf9\u5e94\u7c7b\u578b\u7684\u53cd\u5c04\u8bbe\u7f6e\u503c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u503c\u53ef\u4fee\u6539\u6761\u4ef6\u4e4b\u4e00\uff1a\u53ef\u88ab\u5bfb\u5740<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u53cd\u5c04\u4fee\u6539\u53d8\u91cf\u503c\u7684\u524d\u63d0\u6761\u4ef6\u4e4b\u4e00\uff1a\u8fd9\u4e2a\u503c\u5fc5\u987b\u53ef\u4ee5\u88ab\u5bfb\u5740\u3002\u7b80\u5355\u5730\u8bf4\u5c31\u662f\u8fd9\u4e2a\u53d8\u91cf\u5fc5\u987b\u80fd\u88ab\u4fee\u6539\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"286\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/1-7.png\" alt=\"\" class=\"wp-image-7169\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/1-7.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/1-7-300x121.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/1-7-600x242.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"707\" height=\"92\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/2-7.png\" alt=\"\" class=\"wp-image-7171\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/2-7.png 707w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/2-7-300x39.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/2-7-600x78.png 600w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u503c\u53ef\u4fee\u6539\u6761\u4ef6\u4e4b\u4e00\uff1a\u88ab\u5bfc\u51fa<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ed3\u6784\u4f53\u6210\u5458\u4e2d\uff0c\u5982\u679c\u5b57\u6bb5\u6ca1\u6709\u88ab\u5bfc\u51fa\uff0c \u5373\u4fbf\u4e0d\u4f7f\u7528\u53cd\u5c04\u4e5f\u53ef\u4ee5\u88ab\u8bbf\u95ee\uff0c\u4f46\u4e0d\u80fd\u901a\u8fc7\u53cd\u5c04\u4fee\u6539\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"340\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/3-7.png\" alt=\"\" class=\"wp-image-7174\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/3-7.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/3-7-300x144.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/3-7-600x288.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"706\" height=\"86\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/4-7.png\" alt=\"\" class=\"wp-image-7176\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/4-7.png 706w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/4-7-300x37.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/4-7-600x73.png 600w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u6ce8\u610f\uff1a\u4e0a\u9762\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u5b57\u6bb5\u9996\u5b57\u6bcd\u4e0d\u5927\u5199\uff0c\u5c31\u4f1a\u65e0\u6cd5\u5bfc\u51fa\u5b57\u6bb5\uff0c\u4f1a\u63d0\u793a\u9519\u8befpanic: reflect: reflect.Value.SetInt using value obtained using unexported field\uff0c\u5982\u679c\u7b2c12\u884c\u4e0d\u901a\u8fc7\u7ed3\u6784\u4f53\u6307\u9488\u7c7b\u578b\u83b7\u53d6\u53cd\u5c04\u503c\u5bf9\u8c61\uff0c\u4f1a\u62a5\u9519panic: reflect: reflect.Value.SetInt using unaddressable value<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u503c\u7684\u4fee\u6539\u4ece\u8868\u9762\u610f\u4e49\u4e0a\u53eb\u53ef\u5bfb\u5740\uff0c\u6362\u4e00\u79cd\u8bf4\u6cd5\u5c31\u662f\u503c\u5fc5\u987b\u201c\u53ef\u88ab\u8bbe\u7f6e\u201d\uff0c\u4fee\u6539\u53d8\u91cf\u503c\uff0c\u4e00\u822c\u7684\u6b65\u9aa4\u662f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u53d6\u8fd9\u4e2a\u53d8\u91cf\u7684\u5730\u5740\u6216\u8005\u8fd9\u4e2a\u53d8\u91cf\u6240\u5728\u7684\u7ed3\u6784\u4f53\u5df2\u7ecf\u662f\u6307\u9488\u7c7b\u578b<\/li><li>\u4f7f\u7528reflect.ValueOf \u8fdb\u884c\u503c\u5305\u88c5<\/li><li>\u901a\u8fc7Value.Elem()\u83b7\u5f97\u6307\u9488\u503c\u6307\u5411\u7684\u5143\u7d20\u503c\u5bf9\u8c61(Value)\u56e0\u4e3a\u503c\u5bf9\u8c61(Value),\u5185\u90e8\u5bf9\u8c61\u4e3a\u6307\u9488\u65f6\uff0c \u4f7f\u7528set\u8bbe\u7f6e\u65f6\u4f1a\u62a5\u51fa\u5ca9\u673a\u9519\u8bef<\/li><li>\u4f7f\u7528Value.Set \u8bbe\u7f6e\u503c<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>5\u3001\u901a\u8fc7\u7c7b\u578b\u521b\u5efa\u7c7b\u578b\u7684\u5b9e\u4f8b<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u5df1\u77e5reflect.Type \u65f6\uff0c\u53ef\u4ee5\u52a8\u6001\u5730\u521b\u5efa\u8fd9\u4e2a\u7c7b\u578b\u7684\u5b9e\u4f8b\uff0c\u5b9e\u4f8b\u7684\u7c7b\u578b\u4e3a\u6307\u9488<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff1areflect.Type \u7684\u7c7b\u578b\u4e3aint \u65f6\u2019\uff0c \u521b\u5efaint \u7684\u6307\u9488\uff0c \u5373 *int\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"710\" height=\"293\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/6-7.png\" alt=\"\" class=\"wp-image-7184\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/6-7.png 710w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/6-7-300x124.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/6-7-600x248.png 600w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"706\" height=\"105\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/7-7.png\" alt=\"\" class=\"wp-image-7185\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/7-7.png 706w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/7-7-300x45.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/7-7-600x89.png 600w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>\u7b2c12\u884c\uff1a\u4f7f\u7528reflect.New()\u51fd\u6570\u4f20\u5165\u53d8\u91cfa \u7684\u53cd\u5c04\u7c7b\u578b\u5bf9\u8c61\uff0c\u521b\u5efa\u8fd9\u4e2a\u7c7b\u578b\u7684\u5b9e\u4f8b\u503c\uff0c\u503c\u4ee5reflect.Value \u7c7b\u578b\u8fd4\u56de\u3002\u8fd9\u6b65\u64cd\u4f5c\u7b49\u6548\u4e8e\uff1a new(int\uff09 \uff0c\u56e0\u6b64\u8fd4\u56de\u7684\u662f*int \u7c7b\u578b\u7684\u5b9e\u4f8b<\/li><li>\u7b2c13\u884c\uff1a\u6253\u5370aIns \u7684\u7c7b\u578b\u4e3a *int \uff0c \u79cd\u7c7b\u4e3a\u6307\u9488\u3002<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>6\u3001\u4f7f\u7528\u53cd\u5c04\u8c03\u7528\u51fd\u6570<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u53cd\u5c04\u503c\u5bf9\u8c61\uff08 reflect.Value \uff09\u4e2d\u503c\u7684\u7c7b\u578b\u4e3a\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7reflect. Value \u8c03\u7528\u8be5\u51fd\u6570,\u4f7f\u7528\u53cd\u5c04\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u9700\u8981\u5c06\u53c2\u6570\u4f7f\u7528\u53cd\u5c04\u503c\u5bf9\u8c61\u7684\u5207\u7247[]reflect.Value \u6784\u9020\u540e\u4f20\u5165Call()\u65b9\u6cd5\u4e2d,\u8c03\u7528\u5b8c\u6210\u65f6\uff0c\u51fd\u6570\u7684\u8fd4\u56de\u503c\u901a\u8fc7[]reflect.Value \u8fd4\u56de<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u7684\u4ee3\u7801\u58f0\u660e\u4e00\u4e2a\u52a0\u6cd5\u51fd\u6570\uff0c\u4f20\u5165\u4e24\u4e2a\u6574\u578b\u503c\uff0c\u8fd4\u56de\u4e24\u4e2a\u6574\u578b\u503c\u7684\u548c\uff0c\u5c06\u51fd\u6570\u4fdd\u5b58\u5230\u53cd\u5c04\u503c\u5bf9\u8c61\uff08 reflect.Value \uff09\u4e2d\uff0c\u7136\u540e\u5c06\u4e24\u4e2a\u6574\u578b\u503c\u6784\u9020\u4e3a\u53cd\u5c04\u503c\u5bf9\u8c61\u7684\u5207\u7247([]reflect.Value)\uff0c\u4f7f\u7528Call()\u65b9\u6cd5\u8fdb\u884c\u8c03\u7528\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"333\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/1-8.png\" alt=\"\" class=\"wp-image-7192\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/1-8.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/1-8-300x141.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/1-8-600x282.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"76\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/2-8.png\" alt=\"\" class=\"wp-image-7194\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/2-8.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/2-8-300x32.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/2-8-600x64.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>\u7b2c8\u884c\uff1a\u5b9a\u4e49\u666e\u901a\u7684\u52a0\u6cd5\u51fd\u6570<\/li><li>\u7b2c12\u884c\uff1a\u5c06add\u51fd\u6570\u5305\u88c5\u4e3a\u53cd\u5c04\u503c\u5bf9\u8c61<\/li><li>\u7b2c13\u884c\uff1a\u5c0610\u548c100\u4e24\u4e2a\u6574\u6570\u7528reflect.ValueOf\u5305\u88c5\u4e3areflect.Value\uff0c\u518d\u5c06\u53cd\u5c04\u503c\u5bf9\u8c61\u7684\u5207\u7247[]reflect.Value\u4f5c\u4e3a\u51fd\u6570\u53c2\u6570<\/li><li>\u7b2c14\u884c\uff1a\u4f7f\u7528funcValue \u51fd\u6570\u503c\u5bf9\u8c61\u7684Call()\u65b9\u6cd5\uff0c\u4f20\u5165\u53c2\u6570\u5217\u8868paramLists \u8c03\u7528add()\u51fd\u6570<\/li><li>\u7b2c15\u884c\uff1a\u8c03\u7528\u6210\u529f\u540e\uff0c \u901a\u8fc7resultList[0]\u53d6\u8fd4\u56de\u503c\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u4f7f\u7528Int \u53d6\u8fd4\u56de\u503c\u7684\u6574\u6570\u503c<\/li><\/ul>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u6ce8\u610f\uff1a\u53cd\u5c04\u8c03\u7528\u51fd\u6570\u7684\u8fc7\u7a0b\u9700\u8981\u6784\u9020\u5927\u91cf\u7684reflect.Value \u548c\u4e2d\u95f4\u4ea4\u91cf\uff0c \u5bf9\u51fd\u6570\u53c2\u6570\u503c\u8fdb\u884c\u9010\u4e00\u68c0\u67e5\uff0c \u8fd8\u9700\u8981\u5c06\u8c03\u7528\u53c2\u6570\u590d\u5236\u5230\u8c03\u7528\u51fd\u6570\u7684\u53c2\u6570\u5185\u5b58\u4e2d\u3002\u8c03\u7528\u5b8c\u6bd5\u540e\uff0c\u8fd8\u9700\u8981\u5c06\u8fd4\u56de\u503c\u8f6c\u6362\u4e3areflect.Value \uff0c\u7528\u6237\u8fd8\u9700\u8981\u4ece\u4e2d\u53d6\u51fa\u8c03\u7528\u503c\u3002\u56e0\u6b64\uff0c\u53cd\u5c04\u8c03\u7528\u51fd\u6570\u7684\u6027\u80fd\u95ee\u9898\u5c24\u4e3a\u7a81\u51fa\uff0c\u4e0d\u5efa\u8bae\u5927\u91cf\u4f7f\u7528\u53cd\u5c04\u51fd\u6570\u8c03\u7528\u3002<\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\"><strong>\u4e09\u3001\u5c06\u7ed3\u6784\u4f53\u7684\u6570\u636e\u4fdd\u5b58\u4e3aJSON\u683c\u5f0f\u7684\u6587\u672c\u6570\u636e<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728Go \u8bed\u8a00\u4e2d\uff0c \u7ed3\u6784\u4f53\u53ef\u4ee5\u901a\u8fc7\u7cfb\u7edf\u63d0\u4f9b\u7684json.Marshal()\u51fd\u6570\u8fdb\u884c\u5e8f\u5217\u5316<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c06\u7ed3\u6784\u4f53\u5e8f\u5217\u5316\u4e3aJSON \u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u51c6\u5907\u6570\u636e\u7ed3\u6784\u4f53<\/li><li>\u51c6\u5907\u8981\u5e8f\u5217\u5316\u7684\u7ed3\u6784\u4f53\u6570\u636e<\/li><li>\u8c03\u7528\u5e8f\u5217\u5316\u51fd\u6570<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u7684\u4f8b\u5b50\u8868\u793a\u5c06\u7ed3\u6784\u4f53\u5e8f\u5217\u5316\u4e3ajson\u683c\u5f0f\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"655\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/3-8.png\" alt=\"\" class=\"wp-image-7202\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/3-8.png 709w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/3-8-300x277.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/3-8-600x554.png 600w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"719\" height=\"92\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/4-8.png\" alt=\"\" class=\"wp-image-7204\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/4-8.png 719w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/4-8-300x38.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2022\/02\/4-8-600x77.png 600w\" sizes=\"auto, (max-width: 719px) 100vw, 719px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u53cd\u5c04\u662f\u6307\u5728\u7a0b\u5e8f\u8fd0\u884c\u671f\u5bf9\u7a0b\u5e8f\u672c\u8eab\u8fdb\u884c\u8bbf\u95ee\u548c\u4fee\u6539\u7684\u80fd\u529b\uff0c\u7a0b\u5e8f\u5728\u7f16\u8bd1\u65f6\uff0c\u53d8\u91cf\u88ab\u8f6c\u6362\u4e3a\u5185\u5b58\u5730\u5740\uff0c\u53d8\u91cf\u540d\u4e0d\u4f1a\u88ab\u7f16\u8bd1\u5668\u5199\u5165 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":7065,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111,23],"tags":[],"class_list":["post-7063","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go","category-rong"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":8}},"_links":{"self":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/7063","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/comments?post=7063"}],"version-history":[{"count":117,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/7063\/revisions"}],"predecessor-version":[{"id":10160,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/7063\/revisions\/10160"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media\/7065"}],"wp:attachment":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media?parent=7063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/categories?post=7063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/tags?post=7063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}