\n\u5bfc\u8bfb<\/td>\n | \u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4e3a\u5927\u5bb6\u4ecb\u7ecd\u4e86Go\u8bed\u8a00\u5f00\u53d1\u6846\u67b6\u53cd\u5c04\u673a\u5236\u53ca\u5e38\u89c1\u51fd\u6570\u793a\u4f8b\u8be6\u89e3\uff0c\u6709\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u501f\u9274\u53c2\u8003\u4e0b\uff0c\u5e0c\u671b\u80fd\u591f\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u5927\u5bb6\u591a\u591a\u8fdb\u6b65\uff0c\u65e9\u65e5\u5347\u804c\u52a0\u85aa<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\u57fa\u672c\u4ecb\u7ecd<\/strong><\/div>\n\u53cd\u5c04\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u83b7\u53d6\u53d8\u91cf\u7684\u5404\u79cd\u4fe1\u606f,\u6bd4\u5982\u53d8\u91cf\u7684\u7c7b\u578b,\u7c7b\u522b<\/li>\n\u5982\u679c\u662f\u7ed3\u6784\u4f53\u53d8\u91cf,\u8fd8\u53ef\u4ee5\u83b7\u53d6\u5230\u7ed3\u6784\u4f53\u672c\u8eab\u7684\u4fe1\u606f<\/li>\n\u901a\u8fc7\u53cd\u5c04,\u53ef\u4ee5\u4fee\u6539\u53d8\u91cf\u7684\u503c,\u53ef\u4ee5\u8c03\u7528\u5173\u8054\u7684\u65b9\u6cd5<\/li>\n\u4f7f\u7528\u53cd\u5c04,\u9700\u8981import(\"reflect\")<\/li>\n\u793a\u610f\u56fe<\/p>\n <\/p>\n \u53cd\u5c04\u4e2d\u5e38\u89c1\u51fd\u6570\u548c\u6982\u5ff5<\/strong><\/div>\nreflect.TypeOf(\u53d8\u91cf\u540d)<\/strong><\/span><\/div>\n\u83b7\u53d6\u53d8\u91cf\u7684\u7c7b\u578b,\u8fd4\u56dereflect.Type\u7c7b\u578b<\/p>\n reflect.ValueOf(\u53d8\u91cf\u540d)<\/strong><\/span><\/div>\n\u83b7\u53d6\u53d8\u91cf\u7684\u503c,\u8fd4\u56dereflect.Value\u7c7b\u578breflect.Value\u662f\u4e00\u4e2a\u7ed3\u6784\u4f53\u7c7b\u578b,\u901a\u8fc7reflect.Value,\u53ef\u4ee5\u83b7\u53d6\u5230\u5173\u4e8e\u8be5\u53d8\u91cf\u7684\u5f88\u591a\u4fe1\u606f<\/p>\n \u53d8\u91cf.interface{}\u548creflect.Value\u662f\u53ef\u4ee5\u76f8\u4e92\u8f6c\u6362\u7684<\/strong><\/span><\/div>\n<\/p>\n \u57fa\u672c\u4f7f\u7528<\/strong><\/div>\npackage main\r\nimport (\r\n \"fmt\"\r\n \"reflect\"\r\n)\r\n\/*\r\n1.\u7f16\u5199\u6848\u4f8b,\u5bf9\u57fa\u672c\u6570\u636e\u7c7b\u578b,interface{},reflect.Value\u8fdb\u884c\u53cd\u5c04\r\n2.\u7f16\u5199\u6848\u4f8b,\u5bf9\u7ed3\u6784\u4f53,interface{},reflect.Value\u8fdb\u884c\u53cd\u5c04\r\n*\/\r\nfunc reflectTest(b interface{}){\r\n \/\/\u6253\u5370\u51fa\u4f20\u53c2\u7684type,kind,value\r\n fmt.Printf(\"b\u7684\u7c7b\u578b\u4e3a%v,b\u7684kind\u4e3a%v,value\u4e3a%v\\n\",reflect.TypeOf(b),reflect.ValueOf(b).Kind(),reflect.ValueOf(b)) \/\/b\u7684\u7c7b\u578b\u4e3aint,b\u7684kind\u4e3aint,value\u4e3a100\r\n \/\/reflect.TypeOf(),reflect.ValueOf()\u8fd4\u56de\u7684\u7c7b\u578b\r\n fmt.Printf(\"reflect.TypeOf()\u8fd4\u56de\u7c7b\u578b\u4e3a%T,reflect.ValueOf()\u8fd4\u56de\u7c7b\u578b\u4e3a%T\\n\",reflect.TypeOf(b),reflect.ValueOf(b)) \/\/reflect.TypeOf()\u8fd4\u56de\u7c7b\u578b\u4e3a*reflect.rtype,reflect.ValueOf()\u8fd4\u56de\u7c7b\u578b\u4e3areflect.Value\r\n}\r\ntype Student struct {\r\n Name string\r\n age int\r\n}\r\nfunc reflectTest2(b interface{}){\r\n rTyp:=reflect.TypeOf(b)\r\n fmt.Println(rTyp) \/\/main.Student\r\n rVal:=reflect.ValueOf(b)\r\n \/\/\u5c06rVal\u8f6c\u6362\u6210interface{}\r\n iV:=rVal.Interface()\r\n fmt.Printf(\"iv=%v type=%T\\n\",iV,iV) \/\/iv={\u5f20\u4e09 18} type=main.Student\r\n \/\/\u56e0\u4e3aGo\u8bed\u8a00\u662f\u9759\u6001\u8bed\u8a00,\u6240\u4ee5\u4e0d\u80fd\u76f4\u63a5\u83b7\u53d6\u7ed3\u6784\u4f53\u4e2d\u6307\u5b9a\u7684\u503c,\u6240\u4ee5\u6211\u9700\u8981\u5c06\u5176\u65ad\u8a00\u6210\u9700\u8981\u7684\u7c7b\u578b\r\n stu,ok:=iV.(Student)\r\n if ok{\r\n fmt.Printf(stu.Name,stu.age) \/\/\u5f20\u4e09%!(EXTRA int=18)\r\n }\r\n}\r\nfunc main() {\r\n \/\/1.\u7f16\u5199\u6848\u4f8b,\u5bf9\u57fa\u672c\u6570\u636e\u7c7b\u578b,interface{},reflect.Value\u8fdb\u884c\u53cd\u5c04\r\n var num int =100\r\n reflectTest(num)\r\n \/\/2.\u7f16\u5199\u6848\u4f8b,\u5bf9\u7ed3\u6784\u4f53,interface{},reflect.Value\u8fdb\u884c\u53cd\u5c04\r\n stu:=Student{\r\n Name: \"\u5f20\u4e09\",\r\n age: 18,\r\n }\r\n reflectTest2(stu)\r\n}<\/pre>\n\u53cd\u5c04\u6ce8\u610f\u4e8b\u9879<\/strong><\/div>\nreflect.ValueKind,\u83b7\u53d6\u7684\u53d8\u91cf\u7684\u7c7b\u522b,\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u5e38\u91cf<\/li>\nType\u662f\u7c7b\u578b,Kind\u662f\u7c7b\u522b,Type\u548cKind\u53ef\u80fd\u662f\u76f8\u540c\u7684,\u4e5f\u53ef\u80fd\u662f\u4e0d\u540c\u7684,\u4f8b\u5982\u7ed3\u6784\u4f53<\/li>\n\u901a\u8fc7\u53cd\u5c04\u53ef\u4ee5\u5728\u8ba9\u53d8\u91cf\u5728interface{}\u548creflect.Value\u4e4b\u95f4\u76f8\u4e92\u8f6c\u6362<\/li>\n\u901a\u8fc7\u53cd\u5c04\u7684\u65b9\u5f0f\u83b7\u53d6\u53d8\u91cf\u7684\u503c(\u5e76\u8fd4\u56de\u5bf9\u5e94\u7684\u7c7b\u578b),\u8981\u6c42\u6570\u636e\u7c7b\u578b\u5339\u914d,\u6bd4\u5982x\u662fint,\u90a3\u4e48\u5c31\u5e94\u8be5\u4f7f\u7528reflect.Value(x).Int(),\u800c\u4e0d\u80fd\u4f7f\u7528\u5176\u4ed6\u7684,\u5426\u5219\u62a5panic<\/li>\n\u901a\u8fc7\u53cd\u5c04\u6765\u4fee\u6539\u53d8\u91cf,\u6ce8\u610f\u5f53\u4f7f\u7528Setxxx\u65b9\u6cd5\u6765\u8bbe\u7f6e\u9700\u8981\u901a\u8fc7\u5bf9\u5e94\u7684\u6307\u9488\u7c7b\u578b\u6765\u5b8c\u6210,\u8fd9\u6837\u624d\u80fd\u6539\u53d8\u4f20\u5165\u53d8\u91cf\u7684\u503c,\u540c\u65f6\u9700\u8981\u4f7f\u7528\u5230reflect.Value.Elem()\u65b9\u6cd5<\/li>\n\u53cd\u5c04\u7684\u6700\u4f73\u5b9e\u8df5<\/strong><\/div>\n\u4f7f\u7528\u53cd\u5c04\u6765\u904d\u5386\u7ed3\u6784\u4f53\u7684\u5b57\u6bb5,\u8c03\u7528\u7ed3\u6784\u4f53\u7684\u65b9\u6cd5,\u5e76\u83b7\u53d6\u7ed3\u6784\u4f53\u6807\u7b7e\u7684\u503c<\/p>\n package main\r\nimport (\r\n \"fmt\"\r\n \"reflect\"\r\n)\r\n\/\/\u5b9a\u4e49\u4e86\u4e00\u4e2aMonster\u7ed3\u6784\u4f53\r\ntype Monster struct {\r\n Name string `json:\"name\"`\r\n Age int `json:\"monster_age\"`\r\n Score float32 `json:\"\u6210\u7ee9\"`\r\n Sex string\r\n}\r\n\/\/\u65b9\u6cd5\uff0c\u8fd4\u56de\u4e24\u4e2a\u6570\u7684\u548c\r\nfunc (s Monster) GetSum(n1, n2 int) int {\r\n return n1 + n2\r\n}\r\n\/\/\u65b9\u6cd5\uff0c \u63a5\u6536\u56db\u4e2a\u503c\uff0c\u7ed9s\u8d4b\u503c\r\nfunc (s Monster) Set(name string, age int, score float32, sex string) {\r\n s.Name = name\r\n s.Age = age\r\n s.Score = score\r\n s.Sex = sex\r\n}\r\n\/\/\u65b9\u6cd5\uff0c\u663e\u793as\u7684\u503c\r\nfunc (s Monster) Print() {\r\n fmt.Println(\"---start~----\")\r\n fmt.Println(s)\r\n fmt.Println(\"---end~----\")\r\n}\r\nfunc TestStruct(a interface{}) {\r\n \/\/\u83b7\u53d6reflect.Type \u7c7b\u578b\r\n typ := reflect.TypeOf(a)\r\n \/\/\u83b7\u53d6reflect.Value \u7c7b\u578b\r\n val := reflect.ValueOf(a)\r\n \/\/\u83b7\u53d6\u5230a\u5bf9\u5e94\u7684\u7c7b\u522b\r\n kd := val.Kind()\r\n \/\/\u5982\u679c\u4f20\u5165\u7684\u4e0d\u662fstruct\uff0c\u5c31\u9000\u51fa\r\n if kd != reflect.Struct {\r\n fmt.Println(\"expect struct\")\r\n return\r\n }\r\n \/\/\u83b7\u53d6\u5230\u8be5\u7ed3\u6784\u4f53\u6709\u51e0\u4e2a\u5b57\u6bb5\r\n num := val.NumField()\r\n fmt.Printf(\"struct has %d fields\\n\", num) \/\/4\r\n \/\/\u53d8\u91cf\u7ed3\u6784\u4f53\u7684\u6240\u6709\u5b57\u6bb5\r\n for i := 0; i < num; i++ {\r\n fmt.Printf(\"Field %d: \u503c\u4e3a=%v\\n\", i, val.Field(i))\r\n \/\/\u83b7\u53d6\u5230struct\u6807\u7b7e, \u6ce8\u610f\u9700\u8981\u901a\u8fc7reflect.Type\u6765\u83b7\u53d6tag\u6807\u7b7e\u7684\u503c\r\n tagVal := typ.Field(i).Tag.Get(\"json\")\r\n \/\/\u5982\u679c\u8be5\u5b57\u6bb5\u4e8etag\u6807\u7b7e\u5c31\u663e\u793a\uff0c\u5426\u5219\u5c31\u4e0d\u663e\u793a\r\n if tagVal != \"\" {\r\n fmt.Printf(\"Field %d: tag\u4e3a=%v\\n\", i, tagVal)\r\n }\r\n }\r\n \/\/\u83b7\u53d6\u5230\u8be5\u7ed3\u6784\u4f53\u6709\u591a\u5c11\u4e2a\u65b9\u6cd5\r\n numOfMethod := val.NumMethod()\r\n fmt.Printf(\"struct has %d methods\\n\", numOfMethod)\r\n \/\/var params []reflect.Value\r\n \/\/\u65b9\u6cd5\u7684\u6392\u5e8f\u9ed8\u8ba4\u662f\u6309\u7167 \u51fd\u6570\u540d\u7684\u6392\u5e8f\uff08ASCII\u7801\uff09\r\n val.Method(1).Call(nil) \/\/\u83b7\u53d6\u5230\u7b2c\u4e8c\u4e2a\u65b9\u6cd5\u3002\u8c03\u7528\u5b83\r\n \/\/\u8c03\u7528\u7ed3\u6784\u4f53\u7684\u7b2c1\u4e2a\u65b9\u6cd5Method(0)\r\n var params []reflect.Value \/\/\u58f0\u660e\u4e86 []reflect.Value\r\n params = append(params, reflect.ValueOf(10))\r\n params = append(params, reflect.ValueOf(40))\r\n res := val.Method(0).Call(params) \/\/\u4f20\u5165\u7684\u53c2\u6570\u662f []reflect.Value, \u8fd4\u56de[]reflect.Value\r\n fmt.Println(\"res=\", res[0].Int()) \/\/\u8fd4\u56de\u7ed3\u679c, \u8fd4\u56de\u7684\u7ed3\u679c\u662f []reflect.Value*\/\r\n}\r\nfunc main() {\r\n \/\/\u521b\u5efa\u4e86\u4e00\u4e2aMonster\u5b9e\u4f8b\r\n var a Monster = Monster{\r\n Name: \"\u9ec4\u9f20\u72fc\u7cbe\",\r\n Age: 400,\r\n Score: 30.8,\r\n }\r\n \/\/\u5c06Monster\u5b9e\u4f8b\u4f20\u9012\u7ed9TestStruct\u51fd\u6570\r\n TestStruct(a) \r\n}<\/pre>\n\u4ee5\u4e0a\u5c31\u662fGo\u8bed\u8a00\u5f00\u53d1\u6846\u67b6\u53cd\u5c04\u673a\u5236\u53ca\u5e38\u89c1\u51fd\u6570\u793a\u4f8b\u8be6\u89e3\u7684\u8be6\u7ec6\u5185\u5bb9<\/p>\n","protected":false},"excerpt":{"rendered":" \u53cd\u5c04\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u83b7\u53d6\u53d8\u91cf\u7684\u5404\u79cd\u4fe1\u606f,\u6bd4\u5982\u53d8\u91cf\u7684\u7c7b\u578b,\u7c7b\u522b \u5982\u679c\u662f\u7ed3\u6784\u4f53\u53d8\u91cf,\u8fd8\u53ef\u4ee5\u83b7\u53d6\u5230\u7ed3\u6784\u4f53\u672c\u8eab\u7684\u4fe1\u606f […]<\/p>\n","protected":false},"author":362,"featured_media":244617,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-253835","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/253835","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/362"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=253835"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/253835\/revisions"}],"predecessor-version":[{"id":253840,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/253835\/revisions\/253840"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/244617"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=253835"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=253835"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=253835"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}} |