{"id":130,"date":"2013-07-26T20:37:10","date_gmt":"2013-07-26T12:37:10","guid":{"rendered":"http:\/\/coderbee.net\/?p=130"},"modified":"2013-07-26T20:38:33","modified_gmt":"2013-07-26T12:38:33","slug":"go%e8%af%ad%e8%a8%80%e5%9f%ba%e7%a1%80","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/go\/20130726\/130","title":{"rendered":"Go\u8bed\u8a00\u57fa\u7840"},"content":{"rendered":"<p><strong>\u5206\u53f7<\/strong>  : \u5982\u679c\u5e0c\u671b\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a\u8bed\u53e5\u653e\u5728\u540c\u4e00\u884c\uff0c\u5fc5\u987b\u7528\u5206\u53f7\u5206\u9694\uff1b\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u4e0d\u9700\u8981\u5206\u53f7\u3002<\/p>\n<h2>\u5305<\/h2>\n<p>Go\u7a0b\u5e8f\u662f\u901a\u8fc7\u5305\u6765\u7ec4\u7ec7\u7684\uff0c\u5305\u540d\u4e3a<code>main<\/code>\u7684\u662f\u4e00\u4e2a\u53ef\u72ec\u7acb\u8fd0\u884c\u7684\u5305\uff0c\u7f16\u8bd1\u540e\u4ea7\u751f\u53ef\u6267\u884c\u6587\u4ef6\u3002<code>main<\/code>\u5305\u7684<code>main<\/code>\u51fd\u6570\u662f\u7a0b\u5e8f\u7684\u5165\u53e3\uff0c\u662f\u4e2a\u6ca1\u6709\u53c2\u6570\u4e5f\u6ca1\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u3002<\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528<code>GOPATH<\/code>\u73af\u5883\u53d8\u91cf\u6216\u662f\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u6765import\u81ea\u5b9a\u4e49\u7684package\u3002Go\u7ea6\u5b9a\u5982\u4e0b\uff1a<br \/>\n     1.  \u5728import\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u5982<code>.\/<\/code>\u6216 <code>..\/<\/code>\u6765\u5f15\u7528\u81ea\u5b9a\u4e49\u5305\uff1b<br \/>\n     2.  \u5982\u679c\u6ca1\u6709\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\uff0c\u90a3\u4e48Go\u4f1a\u53bb<code>$GOPATH\/src\/<\/code>\u76ee\u5f55\u67e5\u627e\u3002<br \/>\n<!--more--><\/p>\n<h2>\u53d8\u91cf\u4e0e\u5e38\u91cf<\/h2>\n<h3>\u53d8\u91cf\u58f0\u660e<\/h3>\n<ol>\n<li>\u7c7b\u578b\u662f\u540e\u7f00\u7684\uff0c\u6709\u65f6\u53ef\u4ee5\u7701\u7565\uff1b<\/li>\n<li>\u4ee5var\u5173\u952e\u5b57\u5f00\u5934\uff0c\u6709\u65f6\u53ef\u4ee5\u7701\u7565\u7684\uff1b<\/li>\n<li>\u81ea\u52a8\u7c7b\u578b\u63a8\u65ad\uff1b<\/li>\n<li>\u53ef\u4ee5\u5e73\u884c\u5b9a\u4e49\u591a\u4e2a\u53d8\u91cf\uff1b<\/li>\n<\/ol>\n<p>\u4e3e\u4f8b\uff1a<\/p>\n<pre><code>\nvar count int                                   \/\/ \u58f0\u660e\nvar total  int = 42                            \/\/ \u58f0\u660e\u5e76\u521d\u59cb\u5316\nvar total,  avg int  =  42,  20             \/\/ \u540c\u65f6\u58f0\u660e\u5e76\u521d\u59cb\u5316\u591a\u4e2a\u53d8\u91cf\nvar a, b * int                                   \/\/ \u58f0\u660e\u4e24\u4e2aint\u7c7b\u578b\u6307\u9488\nvar label = \"string value\"                  \/\/ \u7701\u7565\u4e86\u7c7b\u578b\uff0c\u7f16\u8bd1\u5668\u81ea\u52a8\u63a8\u65ad\ni := 123                                          \/\/ \u5b9a\u4e49\u5e76\u521d\u59cb\u5316\ni, str := 123\uff0c \"string value\"             \/\/ \u5e73\u884c\u5b9a\u4e49\uff0c\u8fd8\u53ef\u7528\u7528\u51fd\u6570\u7684\u8fd4\u56de\u503c\u6765\u5b9a\u4e49\n<\/code><\/pre>\n<p><code>:=<\/code>\u53ea\u80fd\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528\uff0c\u5728\u51fd\u6570\u5916\u4f7f\u7528\u4f1a\u51fa\u73b0\u7f16\u8bd1\u9519\u8bef\uff1b\u4e00\u822c\u7528<code>var<\/code>\u65b9\u5f0f\u5b9a\u4e49\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n<p>\u591a\u4e2avar\u58f0\u660e\u53ef\u4ee5\u6210\u7ec4\uff0c\u7ec4\u7528\u5c0f\u62ec\u53f7\uff0cconst\u548cimport\u4e5f\u8fd9\u6837\u505a\u3002\u5982\uff1a<\/p>\n<pre><code>\nimport(\n    \"fmt\"\n    \"os\"\n)\n\nconst(\n    i = 100\n    pi = 3.1415\n    prefix = \"Go_\"\n)\n\nvar(\n    i  int\n    pi  float32\n    prefix  string\n)\n<\/code><\/pre>\n<p>\u5f53\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\u65f6\uff0c\u5b83\u9ed8\u8ba4\u8d4b\u503c\u4e3a\u5176\u7c7b\u578b\u7684<code>null<\/code>\u503c\u3002\u5982\uff0c<code>var a int<\/code>\uff0c<code>a<\/code>\u7684\u503c\u662f<code>0<\/code>\uff0c<code>var s string<\/code>\uff0c <code>s<\/code>\u88ab\u8d4b\u503c\u4e3a\u96f6\u957f\u5ea6\u7684\u5b57\u7b26\u4e32\uff0c\u4e5f\u5c31\u662f<code>\"\"<\/code> \u3002<\/p>\n<p>\u4e00\u4e2a\u7279\u6b8a\u7684\u53d8\u91cf\u540d\u662f <code>_<\/code> \uff08\u4e0b\u5212\u7ebf\uff09\uff0c\u4efb\u4f55\u8d4b\u503c\u7ed9\u5b83\u7684\u503c\u90fd\u88ab\u4e22\u5f03\u3002\u6240\u4ee5 <code>_<\/code> \u53ef\u4ee5\u8bf4\u662f\u5c5e\u4e8e\u4efb\u4f55\u7c7b\u578b\u7684\u3002<\/p>\n<p>\u58f0\u660e\u4f46\u672a\u4f7f\u7528\u7684\u53d8\u91cf\u5728\u7f16\u8bd1\u65f6\u5c06\u62a5\u9519\u3002<\/p>\n<p>###  \u5e38\u91cf<br \/>\n\u5e38\u91cf\u5728\u7f16\u8bd1\u65f6\u88ab\u521b\u5efa\uff0c\u53ea\u80fd\u662f\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u6216\u5e03\u5c14\u503c\u3002<\/p>\n<pre><code>\nconst (\n    a = iota\n    b = iota\n)\n<\/code><\/pre>\n<p><code>iota<\/code>\u53ef\u4ee5\u751f\u6210\u6570\u503c\u7684\u9012\u589e\u7684\u679a\u4e3e\u503c\u3002<code>iota<\/code>\u7b2c\u4e00\u6b21\u4f7f\u7528\u65f6\u662f0\uff0c\u4ee5\u540e\u6bcf\u6b21\u4f7f\u7528\u503c\u589e\u52a01\u3002<\/p>\n<pre><code>\nconst (\n    a = iota\n    b           \/\/ \u9690\u5f0f\u7684 b = iota\n    b string  \/\/ \u660e\u786e\u6307\u5b9a\u7c7b\u578b\n)\n<\/code><\/pre>\n<h2>\u6570\u636e\u7c7b\u578b<\/h2>\n<h3>\u5e03\u5c14\u7c7b\u578b<\/h3>\n<p>\u5e03\u5c14\u7c7b\u578b\u8868\u793a\u7531\u9884\u5b9a\u4e49\u5e38\u91cf<code>true<\/code>\u548c<code>false<\/code>\u4ee3\u8868\u7684\u5e03\u5c14\u5224\u5b9a\u503c\u3002\u5e03\u5c14\u7c7b\u578b\u662f<code>bool<\/code>\u3002<\/p>\n<h3>\u6570\u5b57\u7c7b\u578b<\/h3>\n<p>int \u548c uint\u7c7b\u578b\u7684\u957f\u5ea6\u53d6\u51b3\u4e8e\u786c\u4ef6\u5e73\u53f0\u3002\u4f46\u53ea\u4f1a\u662f32\u621664\u4f4d\u4e4b\u4e00\u3002<\/p>\n<p>\u5b8c\u6574\u7684\u6574\u6570\u7c7b\u578b\u5217\u8868\uff1a<br \/>\n\u6709\u7b26\u53f7\uff1a int8, int16, int 32, int64<br \/>\n\u65e0\u7b26\u53f7\uff1a byte, uint8, uint16, uint32, uint64\u3002<br \/>\nrune \u662fint32\u7684\u522b\u540d\uff0cbyte\u662fuint8\u7684\u522b\u540d\u3002<\/p>\n<p>\u6d6e\u70b9\u7c7b\u578b\u7684\u503c\u6709float32\u548cfloat64\u3002\uff08\u6ca1\u6709float\u7c7b\u578b\uff09<\/p>\n<p>64\u4f4d\u7684\u6574\u6570\u548c\u6d6e\u70b9\u6570\u603b\u662f64\u4f4d\u7684\uff0c\u5373\u4f7f\u662f\u572832\u4f4d\u7684\u67b6\u6784\u4e0a\u3002<\/p>\n<p>\u8fd9\u4e9b\u7c7b\u578b\u90fd\u662f\u72ec\u7acb\u7684\uff0c\u6df7\u5408\u4f7f\u7528\u8fd9\u4e9b\u7c7b\u578b\u5411\u53d8\u91cf\u8d4b\u503c\u4f1a\u5f15\u8d77\u7f16\u8bd1\u5668\u9519\u8bef\u3002<\/p>\n<h3>rune<\/h3>\n<p>rune \u662fint32\u7684\u522b\u540d\uff0c\u7528<code>UTF-8<\/code>\u7f16\u7801\u3002 \u5728\u9700\u8981\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\uff0c\u4e3a\u4e86\u83b7\u5f97\u5b9e\u9645\u7684\u5b57\u7b26\uff0c\u9700\u8981\u4f7f\u7528rune\u7c7b\u578b\u3002<\/p>\n<h3>string<\/h3>\n<p>\u5b57\u7b26\u4e32\u662f\u7528\u53cc\u5f15\u53f7\u6216\u53cd\u5f15\u53f7\u5305\u88f9\u7684\u5b57\u7b26\u5e8f\u5217\uff0c\u662f\u4e0d\u53ef\u53d8\u7684\uff0c<code>UTF-8<\/code>\u7f16\u7801\u3002\u53ef\u4ee5\u7528<code>+<\/code>\u64cd\u4f5c\u7b26\u6765\u8fde\u63a5\u4e24\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u7528\u53cd\u5f15\u53f7<code>\u4f5c\u4e3a\u539f\u59cb\u5b57\u7b26\u4e32\u7b26\u53f7\uff1a<br \/>\n&lt;pre&gt;&lt;code&gt;<br \/>\ns :=<\/code>start part<br \/>\nending part`<br \/>\n<\/code><br \/>\n\u73b0\u5728s\u5305\u542b\u6362\u884c\u3002<\/p>\n<p>\u4e0d\u50cf\u8f6c\u4e49\u5b57\u7b26\u4e32\u6807\u8bc6\uff0c\u539f\u59cb\u5b57\u7b26\u4e32\u6807\u8bc6\u7684\u503c\u5728\u5f15\u53f7\u5185\u7684\u5b57\u7b26\u662f\u4e0d\u8f6c\u4e49\u7684\u3002<\/p>\n<h3>\u590d\u6570<\/h3>\n<p>Go\u539f\u751f\u652f\u6301\u590d\u6570\uff0ccomplex128\u548ccomplex64\uff0c\u865a\u6570\u90e8\u5206\u5206\u522b\u670964\u300132\u4f4d\u3002\u590d\u6570\u5199\u4e3a\uff1a<code>RE + IMi<\/code>\uff0cRE\u662f\u5b9e\u6570\u90e8\u5206\uff0cIM\u662f\u865a\u6570\u90e8\u5206\u3002<\/p>\n<pre><code>\n    var comp complex64 = 4 + 5i\n    fmt.Printf(\"value is : %v\", comp)\n<\/code><\/pre>\n<p>\u5c06\u6253\u5370 <code>(4+5i)<\/code><\/p>\n<h3>\u7c7b\u578b\u8f6c\u6362<\/h3>\n<p>Go\u4e0d\u652f\u6301\u9690\u5f0f\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u6240\u6709\u7684\u7c7b\u578b\u8f6c\u6362\u90fd\u5fc5\u987b\u662f\u663e\u5f0f\u7684\u3002\u7c7b\u578b\u8f6c\u6362\u7684\u65b9\u5f0f\uff1a<code>v  dstType  :=  dstType(srcVal)<\/code>\u3002<\/p>\n<pre><code>\nbytes []byte := []byte(\"string value\")          \/\/ \u5b57\u7b26\u4e32\u5230\u5b57\u8282\u6570\u7ec4\u7684\u8f6c\u6362\nstr := string(bytes)                                   \/\/ \u5b57\u8282\u6570\u7ec4\u5230\u5b57\u7b26\u4e32\u7684\u8f6c\u6362\nui := uint32(-128)                                     \/\/ \u6709\u7b26\u53f7\u6570\u5230\u65e0\u7b26\u53f7\u6570\u7684\u663e\u5f0f\u8f6c\u6362\n<\/code><\/pre>\n<h2>\u8fd0\u7b97\u7b26<\/h2>\n<p><a href=\"\/\/coderbee.net\/wp-content\/uploads\/2013\/07\/go-operator.png\"><img loading=\"lazy\" decoding=\"async\" src=\"\/\/coderbee.net\/wp-content\/uploads\/2013\/07\/go-operator-300x169.png\" alt=\"go-operator\" width=\"300\" height=\"169\" class=\"alignnone size-medium wp-image-324\" srcset=\"https:\/\/coderbee.net\/wp-content\/uploads\/2013\/07\/go-operator-300x169.png 300w, https:\/\/coderbee.net\/wp-content\/uploads\/2013\/07\/go-operator.png 325w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>\u8868\u683c\u4e2d\u6ca1\u6709\u5217\u51fa\u7684\u8fd8\u6709\u903b\u8f91\u975e <code>!<\/code>\u3002<\/p>\n<p><code>&amp;^<\/code> \u8868\u793a\u4f4d\u6e05\u9664\u3002<\/p>\n<p>\u8fd0\u7b97\u7b26\u90fd\u662f\u4ece\u5de6\u5230\u53f3\u7ed3\u5408\u7684\u3002Go\u4e0d\u652f\u6301\u8fd0\u7b97\u7b26\u91cd\u8f7d\u3002<\/p>\n<h2>Go\u4fdd\u7559\u5b57<\/h2>\n<p><a href=\"\/\/coderbee.net\/wp-content\/uploads\/2013\/07\/go-reserve-keyword.png\"><img loading=\"lazy\" decoding=\"async\" src=\"\/\/coderbee.net\/wp-content\/uploads\/2013\/07\/go-reserve-keyword-300x96.png\" alt=\"go-reserve-keyword\" width=\"300\" height=\"96\" class=\"alignnone size-medium wp-image-323\" srcset=\"https:\/\/coderbee.net\/wp-content\/uploads\/2013\/07\/go-reserve-keyword-300x96.png 300w, https:\/\/coderbee.net\/wp-content\/uploads\/2013\/07\/go-reserve-keyword.png 446w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<h2>array\u3001slices\u3001map<\/h2>\n<h3>array<\/h3>\n<p>array \u7531<code>[len]type<\/code> \u5b9a\u4e49\uff0clen\u8868\u793a\u6570\u7ec4\u7684\u957f\u5ea6\uff0ctype\u8868\u793a\u5b58\u50a8\u5143\u7d20\u7684\u7c7b\u578b\u3002\u5bf9array\u5143\u7d20\u7684\u8d4b\u503c\u6216\u7d22\u5f15\u662f\u7531\u62ec\u53f7<code>[]<\/code>\u5b8c\u6210\u7684\u3002<\/p>\n<pre><code>\nvar arr [10]int\narr[0] = 43\narr[1] = 20\n<\/code><\/pre>\n<p>\u5927\u5c0f\u662f\u7c7b\u578b\u7684\u4e00\u90e8\u5206\uff0c\u4e0d\u540c\u5927\u5c0f\u7684\u662f\u4e0d\u540c\u7c7b\u578b\uff0c\u56e0\u6b64\u4e0d\u80fd\u6539\u53d8\u5927\u5c0f\u3002<\/p>\n<p>\u6570\u7ec4\u540c\u6837\u662f\u503c\u7c7b\u578b\uff0c\u5c06\u4e00\u4e2a\u6570\u7ec4\u8d4b\u503c\u7ed9\u53e6\u4e00\u4e2a\u6570\u7ec4\uff0c\u4f1a\u590d\u5236\u6240\u6709\u7684\u5143\u7d20\u3002\u5f53\u5411\u51fd\u6570\u5185\u4f20\u9012\u4e00\u4e2a\u6570\u7ec4\u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u83b7\u5f97\u4e00\u4e2a\u6570\u7ec4\u7684\u526f\u672c\uff0c\u800c\u4e0d\u662f\u6570\u7ec4\u7684\u6307\u9488\u3002<\/p>\n<p><code>[]<\/code>\u5185\u53ef\u663e\u793a\u6307\u5b9a\u6570\u7ec4\u957f\u5ea6\uff0c\u4e5f\u53ef\u7528 <code>...<\/code> \u4ee3\u66ff\uff0c\u5728<code>{}<\/code>\u91cc\u5199\u6570\u7ec4\u5143\u7d20\uff0c\u4f7f\u75283\u4e2a\u53e5\u70b9\u65f6\uff0cGo\u81ea\u52a8\u8ba1\u7b97\u6570\u7ec4\u5143\u7d20\u7684\u4e2a\u6570\u3002<\/p>\n<pre><code>\na := []int {1, 2, 3}                         \/\/ \u58f0\u660e\u4e86\u957f\u5ea6\u4e3a3\u7684\u6570\u7ec4\nb := [10]int{1, 2, 3}                      \/\/ \u58f0\u660e\u4e86\u957f\u5ea6\u4e3a10\u7684\u6570\u7ec4\uff0c\u524d3\u4e2a\u5143\u7d20\u7684\u503c\u5206\u522b\u4e3a1,2,3\uff0c\u5176\u4f59\u5143\u7d20\u7684\u503c\u4e3a0\u3002\nc := [...]int{4, 5, 6}                       \/\/ \u91c7\u7528`...`\u7684\u65b9\u5f0f\uff0cGo\u6839\u636e\u5143\u7d20\u4e2a\u6570\u6765\u8ba1\u7b97\u6570\u7ec4\u957f\u5ea6\u3002\n\na2 := [2][2]int{ [...]int{4, 5}, [...]int{8,9} }          \/\/ \u4e8c\u7ef4\u6570\u7ec4\na3 := [2][2]int{ {4, 5}, {8,9} }                            \/\/ \u4e8c\u7ef4\u6570\u7ec4\uff0c\u7b80\u5316\u7684\u58f0\u660e\u65b9\u5f0f\n<\/code><\/pre>\n<h3>slice<\/h3>\n<p>slice\u4e0earray\u63a5\u8fd1\uff0c\u4f46\u5728\u65b0\u7684\u5143\u7d20\u52a0\u5165\u7684\u65f6\u5019\u53ef\u4ee5\u589e\u52a0\u957f\u5ea6\u3002slice\u603b\u662f\u6307\u5411\u5e95\u5c42\u7684\u4e00\u4e2a\u6570\u7ec4\u3002slice\u662f\u4e00\u4e2a\u6307\u5411\u6570\u7ec4\u7684\u6307\u9488\uff0c\u662f\u5f15\u7528\u7c7b\u578b\u3002slice\u603b\u662f\u4e0e\u4e00\u4e2a\u56fa\u5b9a\u957f\u5ea6\u7684array\u6210\u5bf9\u51fa\u73b0\uff0c\u5176\u5f71\u54cdslice\u7684\u5bb9\u91cf\u548c\u957f\u5ea6\u3002<\/p>\n<p>\u5207\u7247\u53ef\u4ee5\u5728\u6570\u7ec4\u6216\u5df2\u6709\u5207\u7247\u4e0a\u521b\u5efa\uff1a<code>var  s =  arr[start : end]<\/code>\uff0c\u533a\u95f4\u4e3a\uff1a<code>[start,  end)<\/code>\uff0cstart\u548cend\u90fd\u53ef\u4ee5\u7701\u7565\uff0c\u5192\u53f7<code>:<\/code>\u4e0d\u80fd\u7701\u7565\uff0cstart\u9ed8\u8ba4\u4e3a0\uff0cend\u9ed8\u8ba4\u4e3a\u6570\u7ec4\u6216\u5207\u7247\u7684\u957f\u5ea6\u3002<\/p>\n<p><code>s1 := make([]int, 10)<\/code> \u521b\u5efa\u4e00\u4e2a\u4fdd\u5b5810\u4e2a\u5143\u7d20\u7684slice\u3002<\/p>\n<pre><code>\nvar  s  []int                         \/\/  \u58f0\u660e\u4e00\u4e2aslice\uff0c\u4e0d\u9700\u8981\u957f\u5ea6\u3002\ns  :=  []byte{'a', 'b', 'c'}       \/\/  \u58f0\u660e\u5e76\u521d\u59cb\u5316\u3002\n\nvar array = [10]byte {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}     \/\/  \u58f0\u660e\u4e00\u4e2a\u6570\u7ec4\n\nsa :=  array[:]                      \/\/  \u5b9a\u4e49\u4e00\u4e2aslice\uff0c\u5b83\u7684\u5143\u7d20\u662f\u6570\u7ec4ar\u7684\u6240\u6709\u5143\u7d20\u3002\naSlice := array[:3]                \/\/ \u7b49\u4ef7\u4e8eaSlice = array[0:3] aSlice\u5305\u542b\u5143\u7d20: a,b,c\naSlice := array[5:]                \/\/ \u7b49\u4ef7\u4e8eaSlice = array[5:10] aSlice\u5305\u542b\u5143\u7d20: f,g,h,i,j\naSlice := array[:]                  \/\/ \u7b49\u4ef7\u4e8eaSlice = array[0:10] \u8fd9\u6837aSlice\u5305\u542b\u4e86\u5168\u90e8\u7684\u5143\u7d20\n\nvar arr[m] int\nslice := arr[0 : n]\n\u6b64\u65f6\uff0clen(slice) == cap(slice) == n\nlen(arr) == len(arr) == m\n<\/code><\/pre>\n<p>\u5bf9\u4e8eslice\u6709\u51e0\u4e2a\u6709\u7528\u7684\u5185\u7f6e\u51fd\u6570\uff1a<\/p>\n<ul>\n<li><code>len<\/code>\uff1a\u83b7\u53d6slice\u7684\u957f\u5ea6<\/li>\n<li><code>cap<\/code>\uff1a\u83b7\u53d6slice\u7684\u6700\u5927\u5bb9\u91cf<\/li>\n<li><code>append<\/code>\uff1a\u5411slice\u91cc\u9762\u8ffd\u52a0\u4e00\u4e2a\u6216\u8005\u591a\u4e2a\u5143\u7d20\uff0c\u7136\u540e\u8fd4\u56de\u4e00\u4e2a\u548cslice\u4e00\u6837\u7c7b\u578b\u7684slice<\/li>\n<li><code>copy<\/code>\uff1a\u51fd\u6570copy\u4ece\u6e90slice\u7684src\u4e2d\u590d\u5236\u5143\u7d20\u5230\u76ee\u6807dst\uff0c\u5e76\u4e14\u8fd4\u56de\u590d\u5236\u7684\u5143\u7d20\u7684\u4e2a\u6570<\/li>\n<\/ul>\n<p>\u6ce8\uff1a<code>append<\/code>\u51fd\u6570\u4f1a\u6539\u53d8slice\u6240\u5f15\u7528\u7684\u6570\u7ec4\u7684\u5185\u5bb9\uff0c\u4ece\u800c\u5f71\u54cd\u5230\u5f15\u7528\u540c\u4e00\u6570\u7ec4\u7684\u5176\u5b83slice\u3002 \u4f46\u5f53slice\u4e2d\u6ca1\u6709\u5269\u4f59\u7a7a\u95f4\uff08\u5373<code>(cap-len) == 0<\/code>\uff09\u65f6\uff0c\u6b64\u65f6\u5c06\u52a8\u6001\u5206\u914d\u65b0\u7684\u6570\u7ec4\u7a7a\u95f4\u3002\u8fd4\u56de\u7684slice\u6570\u7ec4\u6307\u9488\u5c06\u6307\u5411\u8fd9\u4e2a\u7a7a\u95f4\uff0c\u800c\u539f\u6570\u7ec4\u7684\u5185\u5bb9\u5c06\u4fdd\u6301\u4e0d\u53d8\uff1b\u5176\u5b83\u5f15\u7528\u6b64\u6570\u7ec4\u7684slice\u5219\u4e0d\u53d7\u5f71\u54cd\u3002<\/p>\n<h3>map<\/h3>\n<p>map\u4e5f\u662f\u4e00\u79cd\u5f15\u7528\u7c7b\u578b\u3002\u4e00\u822c\u5b9a\u4e49map\u7684\u65b9\u6cd5\u662f\uff1a <code>map[key_type] value_type<\/code>\u3002<\/p>\n<p><code>mapv := map[string]int {\"key\" : 1, \"key2\" : 2, }<\/code>  \u6700\u540e\u7684\u9017\u53f7\u662f\u5fc5\u987b\u7684\u3002<\/p>\n<p>\u5f53\u53ea\u9700\u8981\u58f0\u660e\u4e00\u4e2amap\u65f6\u5019\uff0c\u4f7f\u7528make\u5f62\u5f0f\uff1a <code>monthdays := make( map[string]int )<\/code>\u3002<\/p>\n<p>\u68c0\u67e5\u5143\u7d20\u662f\u5426\u5b58\u5728\uff0c\u53ef\u7528\u8fd9\u79cd\u5f62\u5f0f\uff1a<code>value, present = monthdays[\"Jan\"]<\/code>\u3002<\/p>\n<p>\u5220\u9664\u5143\u7d20\uff1a  <code>monthdays[\"Mar\"] = 0, false<\/code>\u6216\u8005<code>delete(monthdays,  \"Mar\")<\/code>\u3002<\/p>\n<p>\u6dfb\u52a0\u5143\u7d20\uff1a  <code>monthdays[\"Feb\"] = 29<\/code>\u3002<\/p>\n<h2>\u6d41\u7a0b\u63a7\u5236<\/h2>\n<p>\u6d41\u7a0b\u63a7\u5236\u5206\u4e09\u5927\u7c7b\uff1a\u6761\u4ef6\u5224\u65ad\uff0c\u5faa\u73af\u63a7\u5236\u548c\u65e0\u6761\u4ef6\u8df3\u8f6c\u3002<\/p>\n<p>\u63a7\u5236\u7ed3\u6784\u7684\u6761\u4ef6\u5224\u65ad\u65e0\u9700\u5c0f\u62ec\u53f7\uff0c\u800c\u8bed\u53e5\u4f53\u5fc5\u987b\u603b\u662f\u5305\u542b\u5728\u5927\u62ec\u53f7\u5185\u3002<\/p>\n<h3>\u6761\u4ef6\u8bed\u53e5 if<\/h3>\n<pre><code>\nif [init stmt;] condition {\n    \/\/ true statements\n} else {\n    \/\/ false statements\n}\n\n\/\/  if\u548cswitch\u63a5\u53d7\u521d\u59cb\u5316\u8bed\u53e5\uff0c\u901a\u5e38\u7528\u4e8e\u8bbe\u7f6e\u4e00\u4e2a\uff08\u5c40\u90e8\uff09\u53d8\u91cf\u3002\nif err := file.Chmod(0664); err != nil {        \/\/ \u5de6\u5927\u62ec\u53f7\u5fc5\u987b\u653e\u5728\u884c\u5c3e\n        log.Stderr(err)\n        return err\n}\n<\/code><\/pre>\n<h3>goto<\/h3>\n<p>goto \u8df3\u8f6c\u7684\u76ee\u6807\u4e00\u5b9a\u662f\u5f53\u524d\u51fd\u6570\u5185\u5b9a\u4e49\u7684\u6807\u7b7e\u3002\u6807\u7b7e\u7684\u5b9a\u4e49\uff1a\u5355\u72ec\u4e00\u884c\uff0c\u6b64\u884c\u7b2c\u4e00\u4e2a\u8bcd\uff0c\u4ee5\u5192\u53f7\u7ed3\u675f\uff0c\u6807\u7b7e\u540d\u5927\u5c0f\u5199\u654f\u611f\u3002<\/p>\n<pre><code>\nfunc mufunc() {\n    i := 0\n    Here:        \/\/ \u6b64\u884c\u7b2c\u4e00\u4e2a\u8bcd\uff0c\u4ee5\u5192\u53f7\u7ed3\u675f\u4e3a\u6807\u7b7e\n    println(i)\n    i++\n    goto Here   \/\/ \u8df3\u8f6c\n}\n<\/code><\/pre>\n<h3>\u5faa\u73af\u8bed\u53e5 for<\/h3>\n<p>for\u5faa\u73af\u6709\u4e09\u79cd\u5f62\u5f0f\uff0c\u53ea\u6709\u4e00\u79cd\u4f7f\u7528\u5206\u53f7\u3002<\/p>\n<pre><code>\nfor init; condition; post {}        \/\/  \u548cC\u7684for\u4e00\u6837\nfor condition {}                       \/\/  \u548cwhile\u4e00\u6837\nfor {}                                     \/\/  \u6b7b\u5faa\u73af\n\nfor i, j := 0, len(a) - 1; i < j; i, j = i + 1, j - 1 {        \/\/ \u7ed3\u5408\u4f7f\u7528\u5e73\u884c\u8d4b\u503c\n    a[i], a[j]= a[j], a[i]\n}\n<\/code><\/pre>\n<h4>break\u3001continue<\/h4>\n<p>break\u63d0\u524d\u9000\u51fa\u5faa\u73af\uff0ccontinue\u7ec8\u6b62\u5f53\u524d\u7684\u5faa\u73af\u3002<\/p>\n<p>\u5faa\u73af\u5d4c\u5957\u5faa\u73af\u65f6\uff0c\u53ef\u4ee5\u5728break\u540e\u6307\u5b9a\u6807\u7b7e\uff0c\u7528\u6807\u7b7e\u51b3\u5b9a\u54ea\u4e2a\u5faa\u73af\u88ab\u7ec8\u6b62\u3002<\/p>\n<p>break\u548ccontinue\u8fd8\u53ef\u4ee5\u8ddf\u7740\u6807\u53f7\uff0c\u7528\u6765\u8df3\u5230\u591a\u91cd\u5faa\u73af\u4e2d\u7684\u5916\u5c42\u5faa\u73af\u3002<\/p>\n<h4>range<\/h4>\n<p>range\u53ef\u7528\u4e8e\u5728slice\u3001array\u3001string\u3001map\u3001channel\u4e0a\u5faa\u73af\u3002range\u662f\u4e2a\u8fed\u4ee3\u5668\uff0c\u5f53\u88ab\u8c03\u7528\u7684\u65f6\u5019\uff0c\u4ece\u5b83\u7684\u5faa\u73af\u7684\u5185\u5bb9\u4e2d\u8fd4\u56de\u4e00\u4e2a\u952e\u503c\u5bf9\uff0c\u57fa\u4e8e\u4e0d\u540c\u7684\u5185\u5bb9\uff0crange\u8fd4\u56de\u4e0d\u540c\u4e1c\u897f\u3002<\/p>\n<p>\u5f53\u5bf9slice\u6216array\u505a\u5faa\u73af\u65f6\uff0crange\u8fd4\u56de\u5e8f\u53f7\u4f5c\u4e3a\u952e\uff0c\u5e8f\u53f7\u5bf9\u5e94\u7684\u5185\u5bb9\u4f5c\u4e3a\u503c\u3002<\/p>\n<h3>\u5206\u652f\u8bed\u53e5 switch<\/h3>\n<p>switch \u8868\u8fbe\u5f0f\u4e0d\u5fc5\u662f\u5e38\u91cf\u6216\u6574\u6570\uff0c\u6267\u884c\u7684\u8fc7\u7a0b\u4ece\u4e0a\u81f3\u4e0b\uff0c\u76f4\u5230\u627e\u5230\u5339\u914d\u9879\uff0c\u5982\u679c\u6ca1\u6709\u8868\u8fbe\u5f0f\uff0c\u4f1a\u5339\u914d<code>true<\/code>\u3002<\/p>\n<p><code>fallthrough<\/code> \u53ef\u4ee5\u4f7f\u5339\u914d\u7684\u5206\u652f\u6267\u884c\u5b8c\u540e\u7ee7\u7eed\u6267\u884c\u7d27\u90bb\u7684\u4e0b\u4e00\u4e2a\u5206\u652f\u7684\u8bed\u53e5\u3002<\/p>\n<p>\u5206\u652f\u53ef\u4ee5\u4f7f\u7528\u9017\u53f7\u5206\u9694\u7684\u5217\u8868\uff0c\u8868\u793a\u5339\u914d\u591a\u4e2a\u503c\u4e2d\u7684\u4e00\u4e2a\uff1a<code>case '0', '1' : stements;<\/code>\uff0c\u8868\u793a\u5339\u914d<code>'0', '1'<\/code>\u4e2d\u7684\u4e00\u4e2a\u3002<\/p>\n<p>default\u53ef\u4ee5\u6307\u5b9a\u5176\u4ed6\u6240\u6709\u5206\u652f\u90fd\u4e0d\u5339\u914d\u65f6\u7684\u884c\u4e3a\u3002<\/p>\n<p>\u4e3e\u4f8b\uff1a<\/p>\n<pre><code>\nvar result int\nswitch byte {\ncase 'a', 'b':               \/\/ \u9017\u53f7\u5206\u5272\u5217\u8868\n  result = 1\ndefault:\n  result = 0\n}\n\nswitch result := calculate(); true {\ncase result < 0:                                   \/\/  \u4e0d\u518d\u5c40\u9650\u4e8e\u5339\u914d\u6574\u6570\u3001\u5b57\u7b26\n  \/* negative *\/\ncase result > 0:\n  \/* positive *\/\ndefault:\n  \/* zero *\/\n}\n<\/code><\/pre>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5206\u53f7 : \u5982\u679c\u5e0c\u671b\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a\u8bed\u53e5\u653e\u5728\u540c\u4e00\u884c\uff0c\u5fc5\u987b\u7528\u5206\u53f7\u5206\u9694\uff1b\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u4e0d\u9700\u8981 &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/go\/20130726\/130\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[232],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/130"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=130"}],"version-history":[{"count":7,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/130\/revisions"}],"predecessor-version":[{"id":329,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/130\/revisions\/329"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}