{"id":48655,"date":"2024-12-02T16:21:46","date_gmt":"2024-12-02T08:21:46","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48655\/"},"modified":"2024-12-02T16:21:46","modified_gmt":"2024-12-02T08:21:46","slug":"go-%e4%b8%ad-var-%e5%92%8c-type-%e5%a3%b0%e6%98%8e%e7%bb%93%e6%9e%84%e4%bd%93%e6%9c%89%e4%bb%80%e4%b9%88%e5%8c%ba%e5%88%ab%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48655\/","title":{"rendered":"Go \u4e2d var \u548c type \u58f0\u660e\u7ed3\u6784\u4f53\u6709\u4ec0\u4e48\u533a\u522b\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Go \u4e2d var \u548c type \u58f0\u660e\u7ed3\u6784\u4f53\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/h1>\n<p>\u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300aGo \u4e2d var \u548c type \u58f0\u660e\u7ed3\u6784\u4f53\u6709\u4ec0\u4e48\u533a\u522b\uff1f\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241029\/17301935416720a8851b006.jpg\" class=\"aligncenter\" title=\"Go \u4e2d var \u548c type \u58f0\u660e\u7ed3\u6784\u4f53\u6709\u4ec0\u4e48\u533a\u522b\uff1f\u63d2\u56fe\" alt=\"Go \u4e2d var \u548c type \u58f0\u660e\u7ed3\u6784\u4f53\u6709\u4ec0\u4e48\u533a\u522b\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>golang \u4e2d var \u548c type \u58f0\u660e\u7ed3\u6784\u7684\u533a\u522b<\/strong><\/p>\n<p>\u5bf9\u4e8e go \u65b0\u624b\u6765\u8bf4\uff0c\u533a\u5206 var \u548c type \u58f0\u660e\u7ed3\u6784\u7684\u533a\u522b\u53ef\u80fd\u4ee4\u4eba\u56f0\u60d1\u3002\u4ee5\u4e0b\u8be6\u7ec6\u4ecb\u7ecd\u5b83\u4eec\u7684\u5f02\u540c\uff1a<\/p>\n<p><strong>1. \u76f8\u540c\u70b9<\/strong><\/p>\n<p>\u8fd9\u4e24\u79cd\u8bed\u6cd5\u90fd\u53ef\u4ee5\u7528\u4e8e\u5b9a\u4e49\u4e00\u4e2a\u7ed3\u6784\u4f53\uff0c\u5e76\u4e14\u90fd\u53ef\u4ee5\u5728\u5305\u542b\u533f\u540d\u5b57\u6bb5\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u3002\u533f\u540d\u5b57\u6bb5\u662f\u6307\u6ca1\u6709\u663e\u5f0f\u540d\u79f0\u7684\u5b57\u6bb5\uff0c\u5176\u7c7b\u578b\u4ece\u4e0a\u4e0b\u6587\u4e2d\u63a8\u65ad\u3002<\/p>\n<p><strong>2. \u533a\u522b<\/strong><\/p>\n<p>\u4e3b\u8981\u7684\u533a\u522b\u5728\u4e8e\uff1a<\/p>\n<ul>\n<li><strong>var<\/strong> \u58f0\u660e\u4e00\u4e2a\u533f\u540d\u7ed3\u6784\u4f53\uff0c\u5b83\u53ea\u80fd\u7528\u4e8e\u58f0\u660e\u4e00\u4e2a\u53d8\u91cf\u3002\u8fd9\u610f\u5473\u7740\u4f60\u4e0d\u80fd\u521b\u5efa\u8be5\u7c7b\u578b\u7684\u65b0\u5b9e\u4f8b\u6216\u5c06\u5176\u4f5c\u4e3a\u7c7b\u578b\u4f7f\u7528\u3002<\/li>\n<li><strong>type<\/strong> \u5b9a\u4e49\u4e00\u4e2a\u547d\u540d\u7684\u7ed3\u6784\u7c7b\u578b\uff0c\u5b83\u53ef\u4ee5\u88ab\u7528\u6765\u58f0\u660e\u53d8\u91cf\uff0c\u521b\u5efa\u65b0\u5b9e\u4f8b\uff0c\u5e76\u4f5c\u4e3a\u5176\u4ed6\u7c7b\u578b\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b\u793a\u4f8b\uff1a<\/p>\n<pre>\/\/ \u4f7f\u7528 var \u58f0\u660e\u533f\u540d\u7ed3\u6784\u4f53\nvar people struct {\n    name string\n}\n\n\/\/ \u4f7f\u7528 type \u5b9a\u4e49\u547d\u540d\u7ed3\u6784\u7c7b\u578b\ntype person struct {\n    name string\n}<\/pre>\n<p><strong>\u7b49\u6548\u6027<\/strong><\/p>\n<p>\u4f60\u53ef\u4ee5\u5c06 var people \u58f0\u660e\u7406\u89e3\u4e3a type person \u58f0\u660e\u7684\u7f29\u5199\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>var people = Person{} \/\/ \u7b49\u4ef7\u4e8e var people struct { name string }<\/pre>\n<p><strong>\u533f\u540d\u7ed3\u6784<\/strong><\/p>\n<p>\u8fd9\u79cd\u4f7f\u7528 var \u58f0\u660e\u533f\u540d\u7ed3\u6784\u4f53\u7684\u5199\u6cd5\u7c7b\u4f3c\u4e8e java\/c# \u4e2d\u7684\u533f\u540d\u7c7b\u578b\u3002\u5728 go \u4e2d\uff0c\u6b64\u7c7b\u7ed3\u6784\u88ab\u79f0\u4e3a\u533f\u540d\u7ed3\u6784\uff08anonymous structure\uff09\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u603b\u4e4b\uff0cvar \u7528\u4e8e\u58f0\u660e\u533f\u540d\u7ed3\u6784\u4f53\u53d8\u91cf\uff0c\u800c type \u7528\u4e8e\u5b9a\u4e49\u547d\u540d\u7ed3\u6784\u7c7b\u578b\u3002\u6839\u636e\u4f60\u662f\u5426\u9700\u8981\u4f7f\u7528\u7ed3\u6784\u4f53\u7684\u7c7b\u578b\uff0c\u9009\u62e9\u9002\u5f53\u7684\u8bed\u6cd5\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go \u4e2d var \u548c type \u58f0\u660e\u7ed3\u6784\u4f53\u6709\u4ec0\u4e48\u533a\u522b\uff1f \u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300aGo \u4e2d var \u548c type \u58f0\u660e\u7ed3\u6784\u4f53\u6709\u4ec0\u4e48\u533a\u522b\uff1f\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002 golang \u4e2d var \u548c type \u58f0\u660e\u7ed3\u6784\u7684\u533a\u522b \u5bf9\u4e8e go \u65b0\u624b\u6765\u8bf4\uff0c\u533a\u5206 var \u548c type \u58f0\u660e\u7ed3\u6784\u7684\u533a\u522b\u53ef\u80fd\u4ee4\u4eba\u56f0\u60d1\u3002\u4ee5\u4e0b\u8be6\u7ec6\u4ecb\u7ecd\u5b83\u4eec\u7684\u5f02\u540c\uff1a 1. \u76f8\u540c\u70b9 \u8fd9\u4e24\u79cd\u8bed\u6cd5\u90fd\u53ef\u4ee5\u7528\u4e8e\u5b9a\u4e49\u4e00\u4e2a\u7ed3\u6784\u4f53\uff0c\u5e76\u4e14\u90fd\u53ef\u4ee5\u5728\u5305\u542b\u533f\u540d\u5b57\u6bb5\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u3002\u533f\u540d\u5b57\u6bb5\u662f\u6307\u6ca1\u6709\u663e\u5f0f\u540d\u79f0\u7684\u5b57\u6bb5\uff0c\u5176\u7c7b\u578b\u4ece\u4e0a\u4e0b\u6587\u4e2d\u63a8\u65ad\u3002 2. \u533a\u522b \u4e3b\u8981\u7684\u533a\u522b\u5728\u4e8e\uff1a var \u58f0\u660e\u4e00\u4e2a\u533f\u540d\u7ed3\u6784\u4f53\uff0c\u5b83\u53ea\u80fd\u7528\u4e8e\u58f0\u660e\u4e00\u4e2a\u53d8\u91cf\u3002\u8fd9\u610f\u5473\u7740\u4f60\u4e0d\u80fd\u521b\u5efa\u8be5\u7c7b\u578b\u7684\u65b0\u5b9e\u4f8b\u6216\u5c06\u5176\u4f5c\u4e3a\u7c7b\u578b\u4f7f\u7528\u3002 type \u5b9a\u4e49\u4e00\u4e2a\u547d\u540d\u7684\u7ed3\u6784\u7c7b\u578b\uff0c\u5b83\u53ef\u4ee5\u88ab\u7528\u6765\u58f0\u660e\u53d8\u91cf\uff0c\u521b\u5efa\u65b0\u5b9e\u4f8b\uff0c\u5e76\u4f5c\u4e3a\u5176\u4ed6\u7c7b\u578b\u4f7f\u7528\u3002 \u793a\u4f8b \u8003\u8651\u4ee5\u4e0b\u793a\u4f8b\uff1a \/\/ \u4f7f\u7528 var \u58f0\u660e\u533f\u540d\u7ed3\u6784\u4f53 var people struct { name string } \/\/ \u4f7f\u7528 type \u5b9a\u4e49\u547d\u540d\u7ed3\u6784\u7c7b\u578b type person struct [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-48655","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48655","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=48655"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48655\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48655"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48655"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}