{"id":845,"date":"2023-04-18T16:26:18","date_gmt":"2023-04-18T08:26:18","guid":{"rendered":"https:\/\/white-night.club\/?p=845"},"modified":"2023-11-09T11:07:56","modified_gmt":"2023-11-09T03:07:56","slug":"pg1","status":"publish","type":"post","link":"https:\/\/white-night.club\/index.php\/2023\/04\/18\/pg1\/","title":{"rendered":"Go\u8bed\u8a00\u4e2d\u7684\u7279\u6b8a\u6807\u8bc6\u7b26"},"content":{"rendered":"\n<p>\u6807\u8bc6\u7b26\uff0c\u4f60\u53ef\u4ee5\u7406\u89e3\u4e3a\u53d8\u91cf\u540d\u3002\u5982\u201dint a=10;\u201c\u4e2d\u7684a\u5c31\u662f\u6807\u8bc6\u7b26\u3002\u800c\u5728Go\u8bed\u8a00\u4e2d\u6709\u7740\u8bb8\u591a\u7279\u6b8a\u6807\u8bc6\u7b26\uff0c\u5b83\u4eec\u5206\u522b\u662f\uff1a<\/p>\n\n\n\n<ul>\n<li>\u7a7a\u767d\u6807\u8bc6\u7b26<\/li>\n\n\n\n<li>\u9884\u58f0\u660e\u6807\u8bc6\u7b26<\/li>\n\n\n\n<li>\u5bfc\u51fa\u6807\u8bc6\u7b26<\/li>\n\n\n\n<li>\u552f\u4e00\u6807\u8bc6\u7b26<\/li>\n<\/ul>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-luminous-vivid-amber-color has-text-color\">\n<p>\u7a7a\u767d\u6807\u8bc6\u7b26(Blank identifier)<\/p>\n<cite><em>\u4e0b\u5212\u7ebf &#8221; _ &#8220;<\/em><\/cite><\/blockquote>\n\n\n\n<p>\u5728Go\u8bed\u8a00\u4e2d\uff0c\u4e0d\u5141\u8bb8\u201d\u5b9a\u4e49\u53d8\u91cf\u4f46\u4e0d\u4f7f\u7528\u201c\u7684\u884c\u4e3a\u3002\u4f8b\u5b50\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code has-medium-font-size\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n\tvar str string\n\tstr = \"Hello World\"\n}<\/code><\/pre>\n\n\n\n<p>\u8be5\u4ee3\u7801\u4f1a\u63d0\u793a\u201cstr declared and not used\u201d\uff0c\u5373str\u58f0\u660e\u4f46\u672a\u4f7f\u7528\u3002<\/p>\n\n\n\n<p>\u5bf9\u4e8e\u4e0d\u88ab\u4f7f\u7528\u4e14\u5b58\u5728\u7684\u53d8\u91cf\uff0c\u53ef\u4ee5\u7528\u7a7a\u767d\u6807\u8bc6\u7b26\u8868\u793a\uff0c\u5e76\u4e14\u80fd\u51cf\u5c11\u5185\u5b58\u7a7a\u95f4\u5f00\u9500\u3002\u7a7a\u767d\u6807\u8bc6\u7b26\u5e38\u7528\u4e8e\u4ee5\u4e0b\u4e09\u79cd\u573a\u5408\uff1a<\/p>\n\n\n\n<ol>\n<li>\u9700\u8981\u5bfc\u5165\u67d0\u4e2a\u5305\uff0c\u4f46\u4e0d\u8c03\u7528\u8be5\u5305\u4e2d\u7684\u7684\u53d8\u91cf\u6216\u51fd\u6570\uff0c\u53ea\u6267\u884c\u5305\u7684\u521d\u59cb\u5316\u51fd\u6570init()\u3002\u53ef\u4ee5\u4f7f\u7528import _yourPackageName\u6765\u907f\u514d\u7f16\u8bd1\u9519\u8bef\u3002<\/li>\n\n\n\n<li>\u51fd\u6570\u6709\u591a\u4e2a\u8fd4\u56de\u503c\uff0c\u4f46\u7a0b\u5e8f\u53ea\u4f7f\u7528\u5176\u4e2d\u4e00\u4e2a\u8fd4\u56de\u503c\u3002\u4e0d\u88ab\u4f7f\u7528\u7684\u8fd4\u56de\u503c\u53ef\u4ee5\u7528\u4e0b\u5212\u7ebf &#8220;_&#8221;\u4ee3\u66ff\uff0c\u800c\u4e0d\u7528\u65b0\u5efa\u53d8\u91cf\u3002<\/li>\n\n\n\n<li>\u7c7b\u578b\u65ad\u8a00\uff0c\u5224\u65ad\u67d0\u4e2a\u7c7b\u578b\u662f\u5426\u5b9e\u73b0\u4e86\u63a5\u53e3\u3002<\/li>\n<\/ol>\n\n\n\n<p>1\u548c3\u90fd\u662f\u540e\u9762\u7684\u77e5\u8bc6\u4e86\uff0c\u6211\u4eec\u8fd9\u91cc\u5148\u6765\u770b\u770b2\u7684\u4f7f\u7528\u4f8b\u3002\u4ee3\u7801\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n\tvar str string\n\tstr = \"Hello World\"\n\t_ = str\n\tfmt.Printf(str)\n}\n<\/code><\/pre>\n\n\n\n<p>\u8be5\u7a0b\u5e8f\u80fd\u6b63\u5e38\u8fd0\u884c\uff0c\u8fd4\u56de\u503c\u4e3a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello World\n<\/code><\/pre>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-luminous-vivid-amber-color has-text-color\">\n<p>\u9884\u58f0\u660e\u6807\u8bc6\u7b26(Predeclared identifiers)<\/p>\n<cite><em>int\uff0cbool\uff0cstring\uff0ctrue\u7b49<\/em><\/cite><\/blockquote>\n\n\n\n<p>\u8fd9\u4e2a\u5176\u5b9e\u6ca1\u5565\u597d\u8bf4\u7684\uff0c\u4e3b\u8981\u662fGo\u8bed\u8a00\u4e2d\u5b9a\u4e49\u53d8\u91cf\u7684\u65b9\u5f0f\u6709\u597d\u51e0\u79cd\u3002\u6bd4\u5982<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nfunc main() {\n\tvar num = 10 <mark style=\"background-color:rgba(0, 0, 0, 0);color:#00ff2f\" class=\"has-inline-color\">\/\/\u5b9a\u4e49\u65b9\u5f0f\u4e00<\/mark>\n\t\n\tvar num1 int <mark style=\"background-color:rgba(0, 0, 0, 0);color:#00ff2f\" class=\"has-inline-color\">\/\/\u5b9a\u4e49\u65b9\u5f0f\u4e8c<\/mark>\n\tnum1=20\n\t\n\tnum2:=30 <mark style=\"background-color:rgba(0, 0, 0, 0);color:#00ff2f\" class=\"has-inline-color\">\/\/\u5b9a\u4e49\u65b9\u5f0f\u4e09<\/mark>\n\t\n\t_=num+num1+num2\n}\n<\/code><\/pre>\n\n\n\n<p>\u5b9a\u4e49\u65b9\u5f0f\u4e00\u5728\u4f7f\u7528\u5173\u952e\u5b57var\u7684\u65f6\u5019\u5c31\u5b9a\u4e49\u4e86num=10\u3002\u800c\u5b9a\u4e49\u65b9\u5f0f\u4e8c\u5219\u662f\u5148\u5b9a\u4e49num1\u53d8\u91cf\u4e3aint\u7c7b\u578b\uff0c\u518d\u5b9a\u4e49\u5176\u5177\u4f53\u503c\u3002\u5f88\u660e\u663e\u4e24\u8005\u6709\u7740\u4e0d\u540c\u7684\u5148\u540e\u987a\u5e8f\u3002<\/p>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-luminous-vivid-amber-color has-text-color\">\n<p class=\"has-luminous-vivid-amber-color has-text-color\">\u5bfc\u51fa\u6807\u8bc6\u7b26(Export identifiers)<\/p>\n<cite>\u5f00\u5934\u5b57\u6bcd\u5927\u5199<\/cite><\/blockquote>\n\n\n\n<p>\u5728Go\u8bed\u8a00\u4e2d\u6709\u7684package\u7684\u6982\u5ff5\uff0c\u8fd9\u610f\u5473\u7740\u7a0b\u5e8f\u53ef\u4ee5\u5bfc\u5165\u67d0\u4e2a\u5305\uff0c\u5e76\u8c03\u7528\u8be5\u5305\u4e2d\u7684\u53d8\u91cf\u6216\u51fd\u6570\u65b9\u6cd5\u3002\u9700\u8981\u5bfc\u51fa\u7684\u53d8\u91cf\u6216\u65b9\u6cd5\u53ea\u9700\u8981\u8ba9\u5176\u9996\u4e2a\u5b57\u6bcd\u4ee5\u5927\u5199\u5f00\u5934\uff0c\u5373\u53ef\u88ab\u8c03\u7528\u3002<\/p>\n\n\n\n<p>\u6765\u770b\u4f8b\u5b50\uff0c\u5148\u5b9a\u4e49\u4e00\u4e2anewPackage\u5305\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package newPackage\n\nvar New_var int = 10\nvar new_var int = 10\n<\/code><\/pre>\n\n\n\n<p>\u5728\u4e3b\u51fd\u6570\u4e2d\u5bfc\u5165\u8fd9\u4e2a\u5305\uff0c\u4f1a\u53d1\u73b0\u7f16\u8bd1\u5668\u53ea\u4f1a\u8bc6\u522b\u5230New_var\uff0c\u800c\u4e0d\u4f1a\u8bc6\u522bnew_var\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n\t\"awesomeProject1\/newPackage\"\n\t\"fmt\"\n)\n\nfunc main() {\n\tfmt.Printf(\"%v\", newPackage.New_var)\n       <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-secondary-color\"> <\/mark><em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#00ff2f\" class=\"has-inline-color\">\/\/fmt.Printf(\"%v\", newPackage.new_var) \u4f1a\u62a5\u9519<\/mark><\/em>\n}\n<\/code><\/pre>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-luminous-vivid-amber-color has-text-color\">\n<p>\u552f\u4e00\u6807\u8bc6\u7b26(Uniqueness of identifiers)<\/p>\n<cite>\u4e0d\u5141\u8bb8\u201c\u4e0d\u540c\u7c7b\u578b\u4f46\u540d\u79f0\u76f8\u540c\u201d<\/cite><\/blockquote>\n\n\n\n<p>\u5176\u5b9e\u5c31\u662f\u6807\u8bc6\u7b26\u662f\u552f\u4e00\u7684\uff0c\u5373\u4f7f\u7c7b\u578b\u4e0d\u4e00\u6837\u540d\u79f0\u4e5f\u4e0d\u80fd\u91cd\u590d\u3002\u76f4\u63a5\u770b\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n\t\"fmt\"\n)\n\nfunc main() {\n\tvar name string\n\tvar name int\n\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u62a5\u9519\uff0c\u628a\u5176\u4e2d\u4e00\u4e2aname\u6539\u6210Name\u6216\u8005\u5176\u4ed6\u7684\u540d\u79f0\u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7a7a\u767d\u3001\u9884\u58f0\u660e\u3001\u5bfc\u51fa\u3001\u552f\u4e00<\/p>\n","protected":false},"author":1,"featured_media":3838,"comment_status":"open","ping_status":"closed","sticky":false,"template":"base","format":"standard","meta":{"footnotes":""},"categories":[44,33],"tags":[34],"_links":{"self":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/845"}],"collection":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/comments?post=845"}],"version-history":[{"count":0,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/845\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media\/3838"}],"wp:attachment":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media?parent=845"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/categories?post=845"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/tags?post=845"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}