{"id":1433,"date":"2025-02-08T23:48:24","date_gmt":"2025-02-08T15:48:24","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1433"},"modified":"2025-02-08T23:48:25","modified_gmt":"2025-02-08T15:48:25","slug":"go%e8%af%ad%e8%a8%80%e5%9f%ba%e7%a1%80%ef%bc%9a%e6%8e%8c%e6%8f%a1go%e7%bc%96%e7%a8%8b%e8%af%ad%e6%b3%95%e3%80%81%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e4%b8%8e%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/08\/go%e8%af%ad%e8%a8%80%e5%9f%ba%e7%a1%80%ef%bc%9a%e6%8e%8c%e6%8f%a1go%e7%bc%96%e7%a8%8b%e8%af%ad%e6%b3%95%e3%80%81%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e4%b8%8e%e5%87%bd%e6%95%b0\/","title":{"rendered":"Go\u8bed\u8a00\u57fa\u7840\uff1a\u638c\u63e1Go\u7f16\u7a0b\u8bed\u6cd5\u3001\u6570\u636e\u7c7b\u578b\u4e0e\u51fd\u6570"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u7684\u57fa\u7840\u8bed\u6cd5\u975e\u5e38\u7b80\u6d01\u4e14\u76f4\u89c2\uff0c\u8fd9\u4e5f\u662fGo\u8bed\u8a00\u5e7f\u53d7\u6b22\u8fce\u7684\u91cd\u8981\u539f\u56e0\u4e4b\u4e00\u3002\u5728\u8fd9\u4e00\u90e8\u5206\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecdGo\u8bed\u8a00\u7684\u57fa\u7840\u6982\u5ff5\uff0c\u5305\u62ec\u53d8\u91cf\u58f0\u660e\u3001\u5e38\u91cf\u4e0e\u679a\u4e3e\u3001\u63a7\u5236\u8bed\u53e5\u3001\u6570\u636e\u7c7b\u578b\u3001\u6570\u7ec4\u4e0e\u5207\u7247\u3001\u6620\u5c04\u3001\u6307\u9488\u3001\u7ed3\u6784\u4f53\u3001\u63a5\u53e3\u4ee5\u53ca\u51fd\u6570\u7b49\u5185\u5bb9\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">2.1 <strong>\u57fa\u672c\u8bed\u6cd5<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u7684\u8bed\u6cd5\u8bbe\u8ba1\u7b80\u6d01\u4e14\u6613\u4e8e\u7406\u89e3\uff0c\u5b83\u7701\u53bb\u4e86\u8bb8\u591a\u5197\u957f\u7684\u5173\u952e\u5b57\u548c\u590d\u6742\u7684\u8bed\u6cd5\u7ed3\u6784\uff0c\u4fbf\u4e8e\u5feb\u901f\u5f00\u53d1\u3002Go\u4ee3\u7801\u662f\u4ee5<strong>\u5305\uff08package\uff09<\/strong>\u4e3a\u57fa\u672c\u5355\u4f4d\u8fdb\u884c\u7ec4\u7ec7\u7684\uff0c\u6bcf\u4e2aGo\u6587\u4ef6\u90fd\u5c5e\u4e8e\u67d0\u4e2a\u5305\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f8b\u5b50\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    fmt.Println(\"Hello, Go!\")\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>package main<\/code>\uff1a\u58f0\u660e\u5f53\u524d\u6587\u4ef6\u5c5e\u4e8e<code>main<\/code>\u5305\u3002<\/li>\n\n\n\n<li><code>import \"fmt\"<\/code>\uff1a\u5f15\u5165Go\u6807\u51c6\u5e93\u4e2d\u7684<code>fmt<\/code>\u5305\uff0c\u7528\u4e8e\u683c\u5f0f\u5316\u8f93\u5165\u8f93\u51fa\u3002<\/li>\n\n\n\n<li><code>func main()<\/code>\uff1aGo\u7a0b\u5e8f\u7684\u5165\u53e3\u51fd\u6570\uff0c\u7a0b\u5e8f\u6267\u884c\u4ece<code>main()<\/code>\u51fd\u6570\u5f00\u59cb\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">2.2 <strong>\u53d8\u91cf\u58f0\u660e\u4e0e\u7c7b\u578b<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u91c7\u7528\u9759\u6001\u7c7b\u578b\u7cfb\u7edf\uff0c\u4f46\u5b83\u901a\u8fc7\u7c7b\u578b\u63a8\u65ad\u6765\u7b80\u5316\u53d8\u91cf\u58f0\u660e\uff0c\u65e2\u53ef\u4ee5\u663e\u5f0f\u58f0\u660e\u7c7b\u578b\uff0c\u4e5f\u53ef\u4ee5\u8ba9\u7f16\u8bd1\u5668\u63a8\u65ad\u7c7b\u578b\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">2.2.1 <strong>\u57fa\u672c\u7684\u53d8\u91cf\u58f0\u660e<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u663e\u5f0f\u58f0\u660e\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var x int = 10\nvar name string = \"Go\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7c7b\u578b\u63a8\u65ad\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var x = 10  \/\/ Go\u63a8\u65adx\u4e3aint\u7c7b\u578b<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u77ed\u58f0\u660e\uff08\u5e38\u7528\u4e8e\u51fd\u6570\u5185\u90e8\uff09\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x := 10  \/\/ Go\u63a8\u65adx\u4e3aint\u7c7b\u578b<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">2.2.2 <strong>\u5e38\u91cf\u4e0e\u679a\u4e3e<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u901a\u8fc7<code>const<\/code>\u5173\u952e\u5b57\u5b9a\u4e49\u5e38\u91cf\u3002\u5e38\u91cf\u662f\u4e0d\u53ef\u4fee\u6539\u7684\u503c\uff0c\u53ef\u4ee5\u662f\u57fa\u672c\u7c7b\u578b\u6216\u679a\u4e3e\u7c7b\u578b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const Pi = 3.14\nconst (\n    Red = iota\n    Green\n    Blue\n)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>iota<\/code> \u662fGo\u4e2d\u5e38\u7528\u4e8e\u521b\u5efa\u679a\u4e3e\u7c7b\u578b\u7684\u5173\u952e\u5b57\uff0c\u5b83\u4f1a\u5728\u6bcf\u6b21<code>const<\/code>\u58f0\u660e\u65f6\u81ea\u52a8\u9012\u589e\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">2.3 <strong>\u63a7\u5236\u8bed\u53e5<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u7684\u63a7\u5236\u8bed\u53e5\u4e0eC\u8bed\u8a00\u76f8\u4f3c\uff0c\u4f46\u4e5f\u6709\u4e00\u4e9b\u6539\u8fdb\uff0c\u4f8b\u5982<code>if<\/code>\u8bed\u53e5\u53ef\u4ee5\u5728\u58f0\u660e\u53d8\u91cf\u65f6\u8fdb\u884c\u521d\u59cb\u5316\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">2.3.1 <strong>if\u8bed\u53e5<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>if x &gt; 10 {\n    fmt.Println(\"x is greater than 10\")\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e26\u521d\u59cb\u5316\u8bed\u53e5\u7684if\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if x := 10; x &gt; 5 {\n    fmt.Println(\"x is greater than 5\")\n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">2.3.2 <strong>for\u8bed\u53e5<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u4e2d\u7684<code>for<\/code>\u662f\u552f\u4e00\u7684\u5faa\u73af\u8bed\u53e5\uff0c\u65e2\u53ef\u7528\u4f5c<code>while<\/code>\u5faa\u73af\uff0c\u4e5f\u53ef\u4f5c\u4e3a<code>for<\/code>\u5faa\u73af\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u57fa\u672c\u7684for\u5faa\u73af\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i := 0; i &lt; 10; i++ {\n    fmt.Println(i)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><code>for<\/code>\u4f5c\u4e3a<code>while<\/code>\u4f7f\u7528\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>i := 0\nfor i &lt; 10 {\n    fmt.Println(i)\n    i++\n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">2.3.3 <strong>switch\u8bed\u53e5<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u7684<code>switch<\/code>\u8bed\u53e5\u6bd4C\u8bed\u8a00\u66f4\u4e3a\u7b80\u6d01\uff0c\u4e14\u4e0d\u9700\u8981<code>break<\/code>\u6765\u963b\u6b62\u8d2f\u7a7f\u6267\u884c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>switch x {\ncase 1:\n    fmt.Println(\"One\")\ncase 2:\n    fmt.Println(\"Two\")\ndefault:\n    fmt.Println(\"Other\")\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">2.4 <strong>\u8fd0\u7b97\u7b26<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u652f\u6301\u5e38\u89c1\u7684\u7b97\u672f\u8fd0\u7b97\u7b26\u3001\u5173\u7cfb\u8fd0\u7b97\u7b26\u3001\u903b\u8f91\u8fd0\u7b97\u7b26\u7b49\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">2.4.1 <strong>\u7b97\u672f\u8fd0\u7b97\u7b26<\/strong><\/h5>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>+<\/code>, <code>-<\/code>, <code>*<\/code>, <code>\/<\/code>, <code>%<\/code><\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\">2.4.2 <strong>\u5173\u7cfb\u8fd0\u7b97\u7b26<\/strong><\/h5>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>==<\/code>, <code>!=<\/code>, <code>><\/code>, <code>&lt;<\/code>, <code>>=<\/code>, <code>&lt;=<\/code><\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\">2.4.3 <strong>\u903b\u8f91\u8fd0\u7b97\u7b26<\/strong><\/h5>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>&amp;&amp;<\/code>\uff08\u4e0e\uff09\u3001<code>||<\/code>\uff08\u6216\uff09\u3001<code>!<\/code>\uff08\u975e\uff09<\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\">2.4.4 <strong>\u4f4d\u8fd0\u7b97\u7b26<\/strong><\/h5>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>&amp;<\/code>, <code>|<\/code>, <code>^<\/code>, <code>&lt;&lt;<\/code>, <code>>><\/code><\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\">2.4.5 <strong>\u8d4b\u503c\u8fd0\u7b97\u7b26<\/strong><\/h5>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>=<\/code>, <code>+=<\/code>, <code>-=<\/code>, <code>*=<\/code>, <code>\/=<\/code>, <code>%=<\/code> \u7b49<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">2.5 <strong>\u6570\u636e\u7c7b\u578b<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u5177\u6709\u591a\u79cd\u5185\u7f6e\u6570\u636e\u7c7b\u578b\uff0c\u652f\u6301\u9759\u6001\u7c7b\u578b\u548c\u52a8\u6001\u7c7b\u578b\u7684\u7ed3\u5408\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">2.5.1 <strong>\u57fa\u672c\u7c7b\u578b<\/strong><\/h5>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>int<\/strong>\uff1a\u6574\u6570\u7c7b\u578b\uff0c\u5927\u5c0f\u6839\u636e\u5e73\u53f0\uff0832\u4f4d\u621664\u4f4d\uff09\u4e0d\u540c\u3002<\/li>\n\n\n\n<li><strong>float32 \/ float64<\/strong>\uff1a\u6d6e\u70b9\u6570\u7c7b\u578b\u3002<\/li>\n\n\n\n<li><strong>string<\/strong>\uff1a\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u652f\u6301Unicode\u5b57\u7b26\u3002<\/li>\n\n\n\n<li><strong>bool<\/strong>\uff1a\u5e03\u5c14\u7c7b\u578b\uff0c<code>true<\/code>\u6216<code>false<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>var age int = 30\nvar height float64 = 5.9\nvar name string = \"Go\"\nvar isActive bool = true<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">2.5.2 <strong>\u6570\u7ec4\u4e0e\u5207\u7247\uff08Array &amp; Slice\uff09<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6570\u7ec4\uff1a<\/strong> \u5b9a\u4e49\u540e\u957f\u5ea6\u4e0d\u53ef\u6539\u53d8\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var arr &#91;3]int = &#91;3]int{1, 2, 3}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5207\u7247\uff1a<\/strong> \u662f\u4e00\u4e2a\u52a8\u6001\u6570\u7ec4\uff0c\u957f\u5ea6\u53ef\u4ee5\u53d8\u5316\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>slice := &#91;]int{1, 2, 3}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5207\u7247\u5e38\u89c1\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>slice = append(slice, 4)  \/\/ \u5411\u5207\u7247\u6dfb\u52a0\u5143\u7d20<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">2.5.3 <strong>\u6620\u5c04\uff08map\uff09<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u4e2d\u7684<code>map<\/code>\u662f\u4e00\u4e2a\u54c8\u5e0c\u8868\uff08\u952e\u503c\u5bf9\uff09\u7c7b\u578b\uff0c\u7528\u4e8e\u5b58\u50a8\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var m map&#91;string]int = map&#91;string]int{\n    \"apple\":  3,\n    \"banana\": 5,\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2.6 <strong>\u6307\u9488<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u652f\u6301\u6307\u9488\uff0c\u4f46\u4e0eC\u8bed\u8a00\u4e0d\u540c\uff0c\u5b83\u4e0d\u652f\u6301\u6307\u9488\u8fd0\u7b97\u3002\u6307\u9488\u53ef\u4ee5\u6307\u5411\u4efb\u4f55\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u5e76\u901a\u8fc7<code>*<\/code>\u548c<code>&amp;<\/code>\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var x int = 10\nvar p *int = &amp;x  \/\/ p\u6307\u5411x\u7684\u5730\u5740\nfmt.Println(*p)   \/\/ \u89e3\u5f15\u7528\uff0c\u8f93\u51fa10<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2.7 <strong>\u7ed3\u6784\u4f53\uff08struct\uff09<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u4e2d\u7684\u7ed3\u6784\u4f53\u662f\u7528\u4e8e\u7ec4\u5408\u6570\u636e\u7684\u81ea\u5b9a\u4e49\u7c7b\u578b\uff0c\u53ef\u4ee5\u5305\u542b\u591a\u4e2a\u5b57\u6bb5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Person struct {\n    Name string\n    Age  int\n}\n\np := Person{Name: \"Alice\", Age: 30}\nfmt.Println(p.Name)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ed3\u6784\u4f53\u4e5f\u53ef\u4ee5\u5305\u542b\u65b9\u6cd5\uff0c\u65b9\u6cd5\u7684\u63a5\u6536\u8005\u662f\u7ed3\u6784\u4f53\u7c7b\u578b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func (p Person) Greet() {\n    fmt.Println(\"Hello, \" + p.Name)\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2.8 <strong>\u63a5\u53e3\uff08interface\uff09<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u4e2d\u7684\u63a5\u53e3\u5b9a\u4e49\u4e86\u4e00\u7ec4\u65b9\u6cd5\u7b7e\u540d\uff0c\u4efb\u4f55\u7c7b\u578b\u53ea\u8981\u5b9e\u73b0\u4e86\u63a5\u53e3\u5b9a\u4e49\u7684\u65b9\u6cd5\uff0c\u5c31\u88ab\u8ba4\u4e3a\u5b9e\u73b0\u4e86\u8be5\u63a5\u53e3\u3002Go\u8bed\u8a00\u662f\u57fa\u4e8e\u9690\u5f0f\u5b9e\u73b0\u7684\uff0c\u5373\u4e0d\u9700\u8981\u663e\u5f0f\u58f0\u660e\u4e00\u4e2a\u7c7b\u578b\u5b9e\u73b0\u4e86\u63a5\u53e3\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Speaker interface {\n    Speak() string\n}\n\ntype Person struct {\n    Name string\n}\n\nfunc (p Person) Speak() string {\n    return \"Hello, \" + p.Name\n}\n\nfunc greet(s Speaker) {\n    fmt.Println(s.Speak())\n}\n\np := Person{Name: \"Alice\"}\ngreet(p)  \/\/ \u8f93\u51fa\uff1aHello, Alice<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">2.9 <strong>\u51fd\u6570<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u7684\u51fd\u6570\u975e\u5e38\u7075\u6d3b\uff0c\u53ef\u4ee5\u6709\u591a\u4e2a\u8fd4\u56de\u503c\u548c\u53ef\u53d8\u53c2\u6570\uff0c\u5e76\u4e14\u652f\u6301\u95ed\u5305\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">2.9.1 <strong>\u51fd\u6570\u58f0\u660e\u4e0e\u8c03\u7528<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>func add(a int, b int) int {\n    return a + b\n}\n\nresult := add(3, 4)<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">2.9.2 <strong>\u591a\u8fd4\u56de\u503c<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u652f\u6301\u51fd\u6570\u8fd4\u56de\u591a\u4e2a\u503c\uff0c\u5e38\u7528\u4e8e\u9519\u8bef\u5904\u7406\u548c\u8fd4\u56de\u591a\u4e2a\u8ba1\u7b97\u7ed3\u679c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func divide(a int, b int) (int, int) {\n    return a \/ b, a % b\n}\nquotient, remainder := divide(10, 3)<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">2.9.3 <strong>\u53ef\u53d8\u53c2\u6570\uff08variadic functions\uff09<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u652f\u6301\u53ef\u53d8\u53c2\u6570\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u8c03\u7528\u65f6\u4f20\u9012\u4efb\u610f\u6570\u91cf\u7684\u53c2\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func sum(numbers ...int) int {\n    total := 0\n    for _, number := range numbers {\n        total += number\n    }\n    return total\n}\n\nfmt.Println(sum(1, 2, 3, 4))  \/\/ \u8f93\u51fa10<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">2.9.4 <strong>\u95ed\u5305\uff08closures\uff09<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">\u95ed\u5305\u662f\u6307\u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u5f15\u7528\u5176\u5916\u90e8\u4f5c\u7528\u57df\u7684\u53d8\u91cf\uff0c\u5e76\u4e14\u80fd\u591f\u8bbf\u95ee\u8fd9\u4e9b\u53d8\u91cf\uff0c\u5373\u4f7f\u5916\u90e8\u51fd\u6570\u5df2\u7ecf\u8fd4\u56de\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func adder(x int) func(int) int {\n    return func(y int) int {\n        return x + y\n    }\n}\n\nadd5 := adder(5)\nfmt.Println(add5(10))  \/\/ \u8f93\u51fa15<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">2.10 <strong>\u5305\u548c\u6a21\u5757\u7ba1\u7406<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Go\u8bed\u8a00\u91c7\u7528\u4e86\u5305\uff08package\uff09\u548c\u6a21\u5757\uff08module\uff09\u7684\u7ba1\u7406\u65b9\u5f0f\uff0c\u4f7f\u5f97\u4ee3\u7801\u66f4\u5177\u6a21\u5757\u5316\uff0c\u4f9d\u8d56\u5173\u7cfb\u66f4\u6e05\u6670\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">2.10.1 <strong>\u5bfc\u5165\uff08import\uff09<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728Go\u4e2d\uff0c\u901a\u8fc7<code>import<\/code>\u8bed\u53e5\u5bfc\u5165\u5176\u4ed6\u5305\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import \"fmt\"\n``\n\n`\n\nGo\u652f\u6301\u5bf9\u5305\u522b\u540d\u7684\u5b9a\u4e49\uff1a<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">go<br>import f &#8220;fmt&#8221;<br>f.Println(&#8220;Hello, Go!&#8221;)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>##### 2.10.2 **\u521b\u5efa\u548c\u4f7f\u7528\u5305**\n\n\u5728Go\u4e2d\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5305\u6765\u5c01\u88c5\u903b\u8f91\u548c\u529f\u80fd\u3002\u5305\u7684\u8def\u5f84\u901a\u5e38\u4e0e\u6587\u4ef6\u7cfb\u7edf\u7684\u76ee\u5f55\u7ed3\u6784\u5bf9\u5e94\u3002<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">go<br>\/\/ mypackage\/myfunctions.go<br>package mypackage<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">func Greet(name string) string {<br>return &#8220;Hello, &#8221; + name<br>}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">go<br>import &#8220;mypackage&#8221;<br>mypackage.Greet(&#8220;Alice&#8221;)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>##### 2.10.3 **Go Modules\u548c\u4f9d\u8d56\u7ba1\u7406\uff08go.mod\uff09**\n\nGo Modules\u662fGo\u8bed\u8a00\u5b98\u65b9\u63a8\u8350\u7684\u4f9d\u8d56\u7ba1\u7406\u5de5\u5177\uff0c`go.mod`\u6587\u4ef6\u8bb0\u5f55\u4e86\u9879\u76ee\u6240\u4f9d\u8d56\u7684\u5e93\u7248\u672c\u3002\n\n- \u521d\u59cb\u5316Go\u6a21\u5757\uff1a<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">bash<br>go mod init myproject<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>- \u6dfb\u52a0\u4f9d\u8d56\uff1a<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">bash<br>go get github.com\/gin-gonic\/gin<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>- \u67e5\u770b\u4f9d\u8d56\uff1a<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">bash<br>go list -m all<br>&#8220;`<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u8fd9\u4e00\u90e8\u5206\u7684\u5b66\u4e60\uff0c\u6211\u4eec\u638c\u63e1\u4e86Go\u8bed\u8a00\u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u5305\u62ec\u53d8\u91cf\u3001\u63a7\u5236\u8bed\u53e5\u3001\u6570\u636e\u7c7b\u578b\u3001\u6570\u7ec4\u4e0e\u5207\u7247\u3001\u6620\u5c04\u3001\u6307\u9488\u3001\u7ed3\u6784\u4f53\u3001\u63a5\u53e3\u4ee5\u53ca\u51fd\u6570\u7b49\u5185\u5bb9\u3002\u8fd9\u4e9b\u57fa\u7840\u77e5\u8bc6\u662f\u4f60\u8fdb\u4e00\u6b65\u5b66\u4e60Go\u8bed\u8a00\u8fdb\u9636\u5185\u5bb9\u7684\u6839\u57fa\uff0c\u638c\u63e1\u4e86\u5b83\u4eec\u540e\uff0c\u4f60\u5c06\u80fd\u591f\u7f16\u5199\u7ed3\u6784\u6e05\u6670\u3001\u9ad8\u6548\u4e14\u6613\u4e8e\u7ef4\u62a4\u7684Go\u7a0b\u5e8f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go\u8bed\u8a00\u7684\u57fa\u7840\u8bed\u6cd5\u975e\u5e38\u7b80\u6d01\u4e14\u76f4\u89c2\uff0c\u8fd9\u4e5f\u662fGo\u8bed\u8a00\u5e7f\u53d7\u6b22\u8fce\u7684\u91cd\u8981\u539f\u56e0\u4e4b\u4e00\u3002\u5728\u8fd9\u4e00\u90e8\u5206\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecdGo\u8bed\u8a00\u7684\u57fa [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58],"tags":[],"class_list":["post-1433","post","type-post","status-publish","format-standard","hentry","category-golang"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1433","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1433"}],"version-history":[{"count":2,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1433\/revisions"}],"predecessor-version":[{"id":1454,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1433\/revisions\/1454"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}