{"id":42111,"date":"2024-12-01T13:30:01","date_gmt":"2024-12-01T05:30:01","guid":{"rendered":"https:\/\/fwq.ai\/blog\/42111\/"},"modified":"2024-12-01T13:30:01","modified_gmt":"2024-12-01T05:30:01","slug":"%e4%bd%bf%e7%94%a8%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%ae%bf%e9%97%ae%e5%ad%97%e8%8a%82%e7%b1%bb%e5%9e%8b%e7%9a%84%e7%bb%93%e6%9e%84%e4%bd%93%e5%ad%97%e6%ae%b5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/42111\/","title":{"rendered":"\u4f7f\u7528\u5b57\u7b26\u4e32\u8bbf\u95ee\u5b57\u8282\u7c7b\u578b\u7684\u7ed3\u6784\u4f53\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>\u4f7f\u7528\u5b57\u7b26\u4e32\u8bbf\u95ee\u5b57\u8282\u7c7b\u578b\u7684\u7ed3\u6784\u4f53\u5b57\u6bb5<\/span><\/p>\n<h1>\u4f7f\u7528\u5b57\u7b26\u4e32\u8bbf\u95ee\u5b57\u8282\u7c7b\u578b\u7684\u7ed3\u6784\u4f53\u5b57\u6bb5<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-22 17:36:38<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4f7f\u7528\u5b57\u7b26\u4e32\u8bbf\u95ee\u5b57\u8282\u7c7b\u578b\u7684\u7ed3\u6784\u4f53\u5b57\u6bb5\u300b<\/span>\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6709\u6ca1\u6709\u529e\u6cd5\u7528\u5b57\u7b26\u4e32\u8bbf\u95ee []byte \u7c7b\u578b\u7684\u7ed3\u6784\u4f53\u5b57\u6bb5\uff1f <\/p>\n<pre>package main\n\nimport \"fmt\"\nimport \"reflect\"\n\ntype myStruct struct {\n    string1 []byte\n    string2 []byte\n}\n\nfunc main() {\n    v := myStruct{[]byte(\"text\"), []byte(\"text2\")}\n    fmt.Println(getField(&amp;v, \"string1\"))\n}\n\nfunc getField(v *myStruct, field string) string {\n    r := reflect.ValueOf(v)\n    f := reflect.Indirect(r).FieldByName(field)\n    return f.String()\n}<\/pre>\n<p>\u5728\u6211\u7684\u793a\u4f8b\u4e2d\uff0c\u8f93\u51fa\u5b57\u7b26\u4e32\u662f\uff1a <code>&lt;[]uint8 \u503c&gt;<\/code><\/p>\n<p>\u6211\u5df2\u7ecf\u9605\u8bfb\u4e86\u4ee5\u4e0b\u793a\u4f8b\uff0c\u4f46\u8fd9\u4e9b\u793a\u4f8b\u4ec5\u8bbf\u95ee int \u6216 string \u503c\u3002 <\/p>\n<p>\u6309\u540d\u79f0\u8bbf\u95ee\u7ed3\u6784\u4f53\u5c5e\u6027<\/p>\n<p>https:\/\/socketloop.com\/tutorials\/golang-how-to-get-struct-field-and-value-by-name<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<pre>\npackage main\n\nimport \"fmt\"\nimport \"reflect\"\n\ntype myStruct struct {\n    string1 []byte\n    string2 []byte\n}\n\nfunc main() {\n    v := myStruct{[]byte(\"text\"), []byte(\"text2\")}\n    fmt.Println(getField(&amp;v, \"string1\"))\n}\n\nfunc getField(v *myStruct, field string) string {\n    r := reflect.ValueOf(v)\n    f := reflect.Indirect(r).FieldByName(field)\n    return string(f.Bytes())\n}\n<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u4f7f\u7528\u5b57\u7b26\u4e32\u8bbf\u95ee\u5b57\u8282\u7c7b\u578b\u7684\u7ed3\u6784\u4f53\u5b57\u6bb5\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; \u4f7f\u7528\u5b57\u7b26\u4e32\u8bbf\u95ee\u5b57\u8282\u7c7b\u578b\u7684\u7ed3\u6784\u4f53\u5b57\u6bb5 \u4f7f\u7528\u5b57\u7b26\u4e32\u8bbf\u95ee\u5b57\u8282\u7c7b\u578b\u7684\u7ed3\u6784\u4f53\u5b57\u6bb5 \u6765\u6e90\uff1astackoverflow 2024-04-22 17:36:38 0\u6d4f\u89c8 \u6536\u85cf \u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7\u300a\u4f7f\u7528\u5b57\u7b26\u4e32\u8bbf\u95ee\u5b57\u8282\u7c7b\u578b\u7684\u7ed3\u6784\u4f53\u5b57\u6bb5\u300b\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01 \u95ee\u9898\u5185\u5bb9 \u6709\u6ca1\u6709\u529e\u6cd5\u7528\u5b57\u7b26\u4e32\u8bbf\u95ee []byte \u7c7b\u578b\u7684\u7ed3\u6784\u4f53\u5b57\u6bb5\uff1f package main import &#8220;fmt&#8221; import &#8220;reflect&#8221; type myStruct struct { string1 []byte string2 []byte } func main() { v := myStruct{[]byte(&#8220;text&#8221;), []byte(&#8220;text2&#8221;)} fmt.Println(getField(&amp;v, &#8220;string1&#8221;)) } func getField(v *myStruct, field string) string { r := reflect.ValueOf(v) f := reflect.Indirect(r).FieldByName(field) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-42111","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=42111"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42111\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=42111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=42111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=42111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}