{"id":2547,"date":"2015-11-26T13:13:58","date_gmt":"2015-11-26T05:13:58","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2547"},"modified":"2015-11-26T13:13:58","modified_gmt":"2015-11-26T05:13:58","slug":"go%e8%af%ad%e8%a8%80%e5%ad%a6%e4%b9%a0%e6%9d%82%e8%ae%b0","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2547.html","title":{"rendered":"Go\u8bed\u8a00\u5b66\u4e60\u6742\u8bb0"},"content":{"rendered":"<p>=Start=<\/p>\n<h4>\u7f18\u7531\uff1a<\/h4>\n<p>\u5230\u73b0\u5728\u4e3a\u6b62\u63a5\u89e6Go\u8fd9\u95e8\u8bed\u8a00\u4e5f\u5feb2\u4e2a\u6708\u4e86\uff0c\u4f46\u8fd8\u6ca1\u7528\u8fc7Go\u5199\u8fc7\u4ec0\u4e48\u6bd4\u8f83\u5927\u7684\u7a0b\u5e8f\/\u9879\u76ee\uff08\u4e5f\u8bb8\u4ee5\u540e\u4f1a\u6709\u673a\u4f1a\u5427\uff1f\uff09\u8fd9\u671f\u95f4\u4e5f\u56e0\u4e3a\u5de5\u4f5c\u8f83\u5fd9\u7684\u7f18\u6545\uff0c\u770b\u4e66\u5b66\u4e60Go\u4e5f\u662f\u65ad\u65ad\u7eed\u7eed\u7684\uff0c\u53ea\u662f\u4f1a\u5728\u6ca1\u4e8b\u7684\u65f6\u5019\u53bb\u7f51\u4e0a\u627e\u627e\u4e0d\u9519\u7684Go\u8bed\u8a00\u4ee3\u7801\u6765\u770b\u3001\u8bb0\u5f55\u4e00\u4e9b\u4e0d\u9519\u7684\u5c0f\u6280\u5de7\u4ec0\u4e48\u7684\uff0c\u65f6\u95f4\u957f\u4e86\uff0c\u6ca1\u6709\u8f93\u51fa\uff0c\u8fdb\u4e00\u6b65\u5b66\u4e60Go\u8bed\u8a00\u7684\u70ed\u60c5\u6709\u70b9\u51cf\u5f31\uff0c\u6240\u4ee5\uff0c\u89c9\u5f97\u8be5\u5728\u535a\u5ba2\u4e2d\u8bb0\u5f55\u4e9b\u4ec0\u4e48\uff0c\u8ba9\u81ea\u5df1\u4fdd\u6301\u8fd9\u4efd\u70ed\u60c5\uff1b\u6240\u4ee5\u5728\u53c2\u8003\u5176\u5b83\u6587\u7ae0\u7684\u57fa\u7840\u4e0a\uff0c\u63ba\u6742\u70b9\u81ea\u5df1\u7684\u8bb0\u5f55\uff0c\u5c31\u6709\u4e86\u6b64\u6587\u3002<\/p>\n<h4>\u6742\u8bb0\uff1a<\/h4>\n<h5>#\u5f00\u53d1\u73af\u5883\u548c\u5de5\u5177<\/h5>\n<p>\u8bf4\u660e\uff1a\u5728\u5347\u7ea7Go\u4e4b\u524d\uff0c\u5fc5\u987b\u5148\u79fb\u9664\u65e7\u7684\u7248\u672c\u3002<\/p>\n<h6>\u5f00\u53d1\u73af\u5883\u914d\u7f6e<\/h6>\n<p>\u4e0b\u8f7d\u3001\u89e3\u538b\u3001\u5b89\u88c5\u8bbe\u7f6e\uff1a<\/p>\n<pre class=\"lang:default decode:true\">$ wget https:\/\/storage.googleapis.com\/golang\/go1.5.1.linux-amd64.tar.gz\n$ sudo tar -C \/usr\/local -xzf go1.5.1.linux-amd64.tar.gz\n\n$ mkdir -p $HOME\/go_space #\u5728$HOME\u4e0b\u65b0\u5efa\u4e00\u4e2a\u76ee\u5f55\u4f5c\u4e3aGo\u9879\u76ee\u7684\u5de5\u4f5c\u76ee\u5f55\n$ vim ~\/.profile\n++++\nexport GOROOT=\/usr\/local\/go\nexport GOPATH=$HOME\/go_space\nexport PATH=$PATH:$GOROOT\/bin:$GOPATH\/bin\n++++\n$ source ~\/.profile<\/pre>\n<p>\u6d89\u53ca\u5230\u7684\u73af\u5883\u53d8\u91cf\u8bf4\u660e\uff1a<\/p>\n<p>GOROOT: go\u7684\u5b89\u88c5\u76ee\u5f55\u3002<br \/>\n<strong><span style=\"color: #ff0000;\">GOPATH<\/span><\/strong>: \u00a0go\u7684\u5de5\u4f5c\u76ee\u5f55\uff0c\u6240\u6709<span style=\"color: #ff0000;\">\u901a\u8fc7go get\u4e0b\u8f7d\u7684\u7b2c\u4e09\u65b9\u5e93\u90fd\u4f1a\u4f4d\u4e8e\u8be5\u76ee\u5f55\u4e0b<\/span>\u3002<\/p>\n<h6>\u4e00\u4e9b\u5de5\u5177<\/h6>\n<pre class=\"lang:default decode:true\">go get $url\ngodoc -http=:8000\n\ngo help doc\ngo doc regexp #\u67e5\u770bregexp\u8fd9\u4e2apackage\u7684\u6587\u6863\u4fe1\u606f\ngo doc regexp\/syntax #\u67e5\u770bregexp\/syntax\u8fd9\u4e2apackage\u7684\u6587\u6863\u4fe1\u606f\ngo doc regexp.FindString #\u67e5\u770bregexp.FindString\u8fd9\u4e2amethod\u7684\u6587\u6863\u4fe1\u606f\n\ngodoc builtin\t#\u67e5\u770b\u76f8\u5e94\u7684package\u6587\u6863\ngodoc net\/http\t#\u67e5\u770b\u76f8\u5e94\u7684package\u6587\u6863\n\ngodoc fmt Printf\t\t#\u67e5\u770bfmt.Printf\u5305\u91cc\u9762\u7684\u51fd\u6570\ngodoc -src fmt Printf\t#\u67e5\u770bfmt.Printf\u7684\u4ee3\u7801\ngodoc -help\t\t\t\t#\u67e5\u770b godoc \u5e2e\u52a9\n\ngo build filename.go\ngo run filename.go\ngo test -help\n\ngo tool pprof\t#\u6027\u80fd\u8c03\u8bd5\u5de5\u5177\n\ngofmt -w src\t#\u683c\u5f0f\u5316\u6574\u4e2aGo\u9879\u76ee\nfind . -name \"*.go\" | xargs gofmt -w\t#\u683c\u5f0f\u5316Go\u4ee3\u7801\n\n\u53ef\u4ee5\u7528gdb\u8c03\u8bd5go\u7a0b\u5e8f\uff0c\u56e0\u4e3aGo\u9ed8\u8ba4\u7f16\u8bd1\u9009\u9879\u5c31\u4f1a\u751f\u6210\u8c03\u8bd5\u4fe1\u606f\uff0c\u5176\u4ed6\u57fa\u672c\u548c\u8c03\u8bd5C++\u7a0b\u5e8f\u7c7b\u4f3c\uff0c\u66f4\u591a\u5185\u5bb9\u89c1\u6587\u7ae0\uff1aDebugging Go Code with GDB<\/pre>\n<h5>#\u8bed\u6cd5\u5c0f\u7ed3<\/h5>\n<h6>\u521d\u59cb\u5316\u9ed8\u8ba4\u96f6\u503c<\/h6>\n<p>boolean: false<br \/>\ninteger: 0<br \/>\nfloat: 0.0<br \/>\nstring: &#8220;&#8221;<br \/>\npointer, function, interface, slice, channel, map: nil<\/p>\n<h6>\u5b57\u7b26\u4e32<\/h6>\n<p><span style=\"color: #ff0000;\">\u4e00\u4e2aGo\u8bed\u8a00\u5b57\u7b26\u4e32\u662f\u4e00\u4e2a\u4efb\u610f\u5b57\u8282\u7684<strong>\u5e38\u91cf\u5e8f\u5217<\/strong>\u3002<\/span>Go\u8bed\u8a00\u7684\u5b57\u7b26\u4e32\u7c7b\u578b\u5728\u672c\u8d28\u4e0a\u5c31\u4e0e\u5176\u4ed6\u8bed\u8a00\u7684\u5b57\u7b26\u4e32\u7c7b\u578b\u4e0d\u540c\u3002Java\u7684String\u3001C++\u7684std::string\u4ee5\u53caPython 3\u7684str\u7c7b\u578b\u90fd\u53ea\u662f\u5b9a\u5bbd\u5b57\u7b26\u5e8f\u5217\uff0c\u800c<span style=\"color: #ff0000;\">Go\u8bed\u8a00\u7684\u5b57\u7b26\u4e32\u662f\u4e00\u4e2a\u7528UTF-8\u7f16\u7801\u7684\u53d8\u5bbd\u5b57\u7b26\u5e8f\u5217\uff0c\u5b83\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\u90fd\u7528\u4e00\u4e2a\u6216\u591a\u4e2a\u5b57\u8282\u8868\u793a<\/span>\u3002<\/p>\n<p>Go\u8bed\u8a00\u4e2d\u7684\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u4f7f\u7528\u300c\u53cc\u5f15\u53f7\u300d\u6216\u300c\u53cd\u5f15\u53f7\u300d\u6765\u521b\u5efa\u3002<\/p>\n<h6>\u6570\u7ec4\u548c\u5207\u7247<\/h6>\n<pre class=\"lang:default decode:true \">intSlice := []int{1, 2, 3}\nintArray := [3]int{1, 2, 3}<\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0cintSlice\u662f\u957f\u5ea6\u548c\u5bb9\u91cf\u5747\u4e3a3\u7684slice\uff0cintArray\u662f\u957f\u5ea6\u4e3a3\u7684\u6570\u7ec4\u3002<\/p>\n<p>\u5207\u7247\u7684\u521b\u5efa\u65b9\u5f0f\uff1a<\/p>\n<pre class=\"lang:default decode:true\">make([]Type, length, capacity)\nmake([]Type, length)\n[]Type{}\n[]Type{value1, value2, ..., valueN}\t#\u7528\u8fd9\u79cd\u8bed\u6cd5\u521b\u5efa\u5207\u7247\u65f6\u53ef\u4ee5\u8fdb\u884c\u521d\u59cb\u5316\u64cd\u4f5c<\/pre>\n<h6>\u4e8c\u7ef4\u6570\u7ec4\u548c\u5207\u7247\u7684\u521d\u59cb\u5316<\/h6>\n<pre class=\"lang:default decode:true\">func 2d_array_slice() {\n\t\/\/ Initialize a 2D array\n\tvar grid [6][3]int\n\tfmt.Println(grid)\n\t\/\/ [[0 0 0] [0 0 0] [0 0 0] [0 0 0] [0 0 0] [0 0 0]]\n\n\t\/\/ Another way to initialize 2D array\n\tgridB := [6][3]int{}\n\tfmt.Println(gridB)\n\t\/\/ [[0 0 0] [0 0 0] [0 0 0] [0 0 0] [0 0 0] [0 0 0]]\n\n\t\/\/ Initialize a slice of empty slices\n\tgridC := make([][]int, 6)\n\tfmt.Println(gridC)\n\t\/\/ [[] [] [] [] [] []]\n\tfor i := 0; i &lt; len(gridC); i++ {\n\t\tgridC[i] = make([]int, 3)\n\t}\n\tfmt.Println(gridC)\n\t\/\/ [[0 0 0] [0 0 0] [0 0 0] [0 0 0] [0 0 0] [0 0 0]]\n}<\/pre>\n<h6>slice\u548cappend<\/h6>\n<p>\u4f7f\u7528append\u65f6\uff0c\u5982\u679cslice\u7684\u5bb9\u91cf(\u5373\u5bf9\u5e94array\u7684\u957f\u5ea6)\u4e0d\u591f\uff0cGo\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684array(\u957f\u5ea6\u901a\u5e38\u4e3a\u4e4b\u524d\u7684\u4e24\u500d)\u4ee5\u5bb9\u7eb3\u65b0\u6dfb\u52a0\u7684\u6570\u636e\uff0c\u6240\u6709\u65e7\u7684array\u6570\u636e\u90fd\u4f1a\u88ab\u62f7\u8d1d\u5230\u65b0\u7684array\u91cc\u3002<strong><span style=\"color: #ff0000;\">\u5f53\u9891\u7e41\u4f7f\u7528append\u65f6\uff0c\u9700\u8981\u8003\u8651\u5230\u5176\u6548\u7387\u95ee\u9898<\/span><\/strong>\u3002<\/p>\n<pre class=\"lang:default decode:true\">\/\/ \u5982\u679cappend\u4e4b\u524d\u5df2\u7ecf\u77e5\u9053\u957f\u5ea6\uff0c\u53ef\u4ee5\uff1a\nintSlice := make([]int)\nfor i := 0; i &lt; length; i++ {\n    intSlice = append(intSlice, i)\n}\n\n\/\/ \u5f53\u7136\uff0c\u6700\u597d\u662f\u4e0d\u7528append:\nintSlice := make([]int, 0, length)\nfor i := 0; i &lt; length; i++ {\n    intSlice[i] = i\n}<\/pre>\n<h6>\u6620\u5c04<\/h6>\n<p>\u6620\u5c04\u7684\u521b\u5efa\u65b9\u5f0f\uff1a<\/p>\n<pre class=\"lang:default decode:true \">make(map[keyType]valueType, initCapacity)\nmake(map[keyType]valueType)\nmap[keyType]valueType{}\nmap[keyType]valueType{key1: value1, key2: value2, ..., keyN:valueN}\t#\u7528\u8fd9\u79cd\u8bed\u6cd5\u521b\u5efa\u6620\u5c04\u65f6\u53ef\u4ee5\u8fdb\u884c\u521d\u59cb\u5316\u64cd\u4f5c<\/pre>\n<p>\u6839\u636ekey\u53d6value\u7684\u503c\uff1a<\/p>\n<pre class=\"lang:default decode:true \">value, found := aMap[key]<\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0cvalue\u662f\u6620\u5c04\u4e2dkey\u5bf9\u5e94\u7684\u503c\u7684\u62f7\u8d1d\u3002\u5982\u679c\u4e0d\u4f7f\u7528\u7b2c\u4e8c\u4e2a\u53c2\u6570found\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:default decode:true \">value := aMap[key]<\/pre>\n<p>\u5219\u5f53key\u4e0d\u5b58\u5728\u65f6\uff0cvalue\u88ab\u521d\u59cb\u5316\u4e3a\u5bf9\u5e94\u7684\u96f6\u503c\u3002<\/p>\n<h6>\u51fd\u6570\u548c\u65b9\u6cd5<\/h6>\n<p>\u51fd\u6570\uff1a<\/p>\n<pre class=\"lang:default decode:true\">func Getwd() (dir string, err error)\nfunc Hostname() (name string, err error)<\/pre>\n<p>\u65b9\u6cd5\uff1a<\/p>\n<pre class=\"lang:default decode:true\">func (f *File) Read(b []byte) (n int, err error)\nfunc (f *File) Stat() (FileInfo, error)\n\ntype File struct {\n        \/\/ contains filtered or unexported fields\n}<\/pre>\n<h6>\u65b9\u6cd5\u548c\u51fd\u6570\u7684\u5f02\u540c<\/h6>\n<p><strong><span style=\"color: #ff0000;\">\u65b9\u6cd5\u662f\u4f5c\u7528\u5728\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684\u503c\u4e0a\u7684\u4e00\u7c7b\u7279\u6b8a\u51fd\u6570\uff0c\u901a\u5e38\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684\u503c\u4f1a\u88ab\u4f20\u9012\u7ed9\u8be5\u51fd\u6570\u3002<\/span><\/strong><br \/>\n\u8be5\u503c\u53ef\u4ee5\u4ee5\u6307\u9488\u6216\u8005\u503c\u7684\u5f62\u5f0f\u8fdb\u884c\u4f20\u9012\uff0c\u8fd9\u53d6\u51b3\u4e8e\u65b9\u6cd5\u662f\u5982\u4f55\u5b9a\u4e49\u7684\u3002\u5b9a\u4e49\u65b9\u6cd5\u7684\u8bed\u6cd5\u51e0\u4e4e\u7b49\u540c\u4e8e\u5b9a\u4e49\u51fd\u6570\uff0c\u9664\u4e86\u9700\u8981\u5728 func \u5173\u952e\u5b57\u548c\u65b9\u6cd5\u540d\u4e4b\u95f4\u5199\u4e0a\u63a5\u6536\u8005(\u653e\u5728\u62ec\u53f7\u4e2d)\u4e4b\u5916\u3002<\/p>\n<h6>\u53d8\u53c2\u51fd\u6570<\/h6>\n<p><span style=\"color: #ff0000;\">\u6240\u8c13\u53ef\u53d8\u53c2\u6570\u51fd\u6570\u5c31\u662f\u6307\u51fd\u6570\u7684\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u53ef\u4ee5\u63a5\u53d7\u4efb\u610f\u4e2a\u53c2\u6570\u3002<\/span>\u8fd9\u7c7b\u51fd\u6570\u5728\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u7684\u7c7b\u578b\u524d\u9762\u6dfb\u52a0\u6709\u4e00\u4e2a\u7701\u7565\u53f7\u3002\u5728\u51fd\u6570\u91cc\u9762\u8fd9\u4e2a\u53c2\u6570\u5b9e\u8d28\u4e0a\u53d8\u6210\u4e86\u4e00\u4e2a\u5bf9\u5e94\u53c2\u6570\u7c7b\u578b\u7684\u5207\u7247\u3002<\/p>\n<pre class=\"lang:default decode:true\">\u4efb\u610f\u4e2aint\u578b\u53c2\u6570\u7684\u51fd\u6570\t\tfunc MinimunInt0(ints ...int) int {}\n\u81f3\u5c11\u9700\u8981\u4e00\u4e2aint\u578b\u7684\u53c2\u6570\tfunc MinimunInt1(first int, rest ...int) int {}\n\u81f3\u5c11\u9700\u8981\u4e24\u4e2aint\u578b\u7684\u53c2\u6570\tfunc MinimunInt2(first, second int, rest ...int) int {}<\/pre>\n<h6>init() \u548c main() \u51fd\u6570<\/h6>\n<p>\u4e00\u4e2apackage\u91cc\u53ef\u4ee5\u6709\u5f88\u591a init() \u51fd\u6570\uff0c\u4f46\u662f\u4e00\u4e2aGo\u7a0b\u5e8f\u6709\u4e14\u53ea\u80fd\u6709\u4e00\u4e2a main() \u51fd\u6570\u3002<\/p>\n<p>init() \u548c main() \u51fd\u6570\u662f\u81ea\u52a8\u6267\u884c\u7684\uff0c\u4e14 <strong><span style=\"color: #0000ff;\">init() \u51fd\u6570\u5148\u4e8e main() \u51fd\u6570\u6267\u884c<\/span><\/strong>\u3002<\/p>\n<h6>\u63a5\u53e3<\/h6>\n<p><span style=\"color: #0000ff;\"><strong>\u5728Go\u8bed\u8a00\u4e2d\uff0c\u63a5\u53e3\u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7c7b\u578b<\/strong><\/span>\uff0c\u5b83\u58f0\u660e\u4e86\u4e00\u4e2a\u6216\u8005\u591a\u4e2a\u65b9\u6cd5\u7b7e\u540d\u3002<br \/>\n<span style=\"color: #0000ff;\"><strong>\u63a5\u53e3\u662f\u5b8c\u5168\u62bd\u8c61\u5316\u7684\uff0c\u56e0\u6b64\u4e0d\u80fd\u5c06\u5176\u5b9e\u4f8b\u5316<\/strong><\/span>\u3002\u7136\u800c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5176\u7c7b\u578b\u4e3a\u63a5\u53e3\u7684\u53d8\u91cf\uff0c\u5b83\u53ef\u4ee5\u88ab\u8d4b\u503c\u4e3a\u4efb\u4f55\u6ee1\u8db3\u8be5\u63a5\u53e3\u7c7b\u578b\u7684\u5b9e\u9645\u7c7b\u578b\u7684\u503c\u3002<\/p>\n<h6>\u503c\u4f20\u9012<\/h6>\n<p>Go\u8bed\u8a00\u4e2d\u51fd\u6570\u7684\u53c2\u6570\u90fd\u662f\u901a\u8fc7\u503c\u4f20\u9012\u7684\uff01As in all languages in the C family, everything in Go is passed by value.<\/p>\n<h6>\u5f15\u7528\u7c7b\u578b<\/h6>\n<p>Go\u8bed\u8a00\u4e2d\u7684\u5f15\u7528\u7c7b\u578b\uff1a\u6620\u5c04\u3001\u5207\u7247\u3001\u901a\u9053\u3001\u51fd\u6570\u548c\u65b9\u6cd5\u3002\u5728\u53d1\u751f\u503c\u62f7\u8d1d\u65f6\uff0c\u88ab\u62f7\u8d1d\u7684\u4ec5\u4ec5\u662f\u6307\u5411\u5b9e\u9645\u6570\u636e\u7684\u6307\u9488\u3002<\/p>\n<h6>Go\u8bed\u8a00\u4e2d\u7684\u6307\u9488<\/h6>\n<p>Go\u8bed\u8a00\u63d0\u4f9b\u4e86\u4e24\u79cd\u521b\u5efa\u53d8\u91cf\u7684\u8bed\u6cd5\uff0c\u540c\u65f6\u83b7\u5f97\u6307\u5411\u5b83\u4eec\u7684\u6307\u9488\u3002<br \/>\n\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u5185\u7f6e\u7684 new() \u51fd\u6570\uff1b\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u5730\u5740\u64cd\u4f5c\u7b26 &amp; \u3002new(Type) == &amp;Type{}<\/p>\n<p><span style=\"color: #0000ff;\">Go\u8bed\u8a00\u4e0d\u63d0\u4f9b\u6307\u9488\u7684\u8fd0\u7b97\u64cd\u4f5c\u3002<\/span><\/p>\n<p>\u4f7f\u7528\u6307\u9488\u7684\u597d\u5904\u5728\u4e8e\uff1a\u503c\u5728\u4f20\u9012\u7ed9\u51fd\u6570\u6216\u8005\u65b9\u6cd5\u7684\u65f6\u5019\u4f1a\u88ab\u590d\u5236\u4e00\u6b21\u3002\u56e0\u6b64\u6309\u503c\u4f20\u9012 \u5e03\u5c14\u53d8\u91cf\u3001\u6570\u503c\u7c7b\u578b\u3001\u5b57\u7b26\u4e32(Go\u8bed\u8a00\u4e2d\u7684\u5b57\u7b26\u4e32\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u56e0\u6b64\u53ef\u4ee5\u8fdb\u884c\u4f18\u5316) \u65f6\u4f1a\u6bd4\u8f83\u5ec9\u4ef7\u3002\u4f46\u662f\u5728\u4f20\u9012\u5927\u6570\u7ec4\u7684\u65f6\u5019\u4ee3\u4ef7\u4f1a\u975e\u5e38\u5927\u3002\u8fd9\u4e2a\u65f6\u5019\u53ef\u4ee5\u8003\u8651\u5207\u7247(\u5f15\u7528)\u3002<strong><span style=\"color: #0000ff;\">\u901a\u8fc7\u4f7f\u7528\u6307\u9488\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba9\u53c2\u6570\u7684\u4f20\u9012\u6210\u672c\u6700\u4f4e\u5e76\u4e14\u5185\u5bb9\u53ef\u4fee\u6539\uff0c\u800c\u4e14\u8fd8\u53ef\u4ee5\u8ba9\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\u72ec\u7acb\u4e8e\u4f5c\u7528\u57df\u3002<\/span><\/strong>\u6307\u9488\u662f\u6307\u4fdd\u5b58\u4e86\u53e6\u4e00\u4e2a\u53d8\u91cf\u5185\u5b58\u5730\u5740\u7684\u53d8\u91cf\u3002\uff08<span style=\"color: #ff0000;\">\u5f15\u7528\uff0c\u4e5f\u5373\u4fdd\u5b58\u6307\u9488\u7684\u53d8\u91cf<\/span>\uff09<\/p>\n<h6>Go\u8bed\u8a00\u4e2d\u7684\u5e76\u53d1<\/h6>\n<p>Go\u8bed\u8a00\u4e2d\u7684\u5e76\u53d1\u662f\u901a\u8fc7goroutine\u6765\u5b9e\u73b0\u7684\u3002<\/p>\n<p>goroutine\u662f\u7a0b\u5e8f\u4e2d\u4e0e\u5176\u4ed6goroutine\u5b8c\u5168\u76f8\u4e92\u72ec\u7acb\u800c\u5e76\u53d1\u6267\u884c\u7684\u51fd\u6570\u6216\u8005\u65b9\u6cd5\u8c03\u7528\u3002\u6bcf\u4e00\u4e2a\u7a0b\u5e8f\u81f3\u5c11\u5305\u542b\u4e00\u4e2agoroutine\uff0c\u5373\u4f1a\u6267\u884cmain\u5305\u4e2d\u7684main()\u51fd\u6570\u7684\u4e3bgoroutine\u3002\u6240\u6709\u7684goroutine\u5171\u4eab\u76f8\u540c\u7684\u5730\u5740\u7a7a\u95f4\uff0c\u540c\u65f6Go\u8bed\u8a00\u63d0\u4f9b\u4e86\u9501\u539f\u8bed\u6765\u4fdd\u8bc1\u6570\u636e\u80fd\u591f\u5b89\u5168\u7684\u8de8goroutine\u5171\u4eab\u3002\u7136\u800c\uff0cGo\u8bed\u8a00\u63a8\u8350\u7684\u5e76\u53d1\u65b9\u5f0f\u662f\u901a\u4fe1(\u901a\u9053channel)\uff0c\u800c\u975e\u5171\u4eab\u6570\u636e\u3002<\/p>\n<h5>#\u5e38\u89c1\u95ee\u9898<\/h5>\n<h6>\u8d4b\u503c\u64cd\u4f5c\u7b26<\/h6>\n<p>\u4f7f\u7528\u8d4b\u503c\u64cd\u4f5c\u7b26\u300c:=\u300d\u9700\u8981\u6ce8\u610f\u7684\u5730\u65b9\uff1a<\/p>\n<ul>\n<li>\u5de6\u8fb9\u7684\u6210\u5458\u81f3\u5c11\u8981\u6709\u4e00\u4e2a\u662f\u672a\u58f0\u660e\u7684\u53d8\u91cf\u3002<\/li>\n<li>\u5df2\u58f0\u660e\u7684\u53d8\u91cf\uff0c\u5728\u540c\u4e00\u4f5c\u7528\u57df\u5185\uff0c\u53d8\u91cf\u503c\u88ab\u6539\u5199\u3002<\/li>\n<li>\u5982\u679c\u5df2\u58f0\u660e\u7684\u53d8\u91cf\u4f5c\u7528\u57df\u5728\u5916\u5c42\uff0c\u5219\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u540c\u540d\u53d8\u91cf\uff0c\u4f1a\u5c4f\u853d\u5916\u5c42\u7684\u53d8\u91cf\u3002<\/li>\n<\/ul>\n<h6>for&#8230;range<\/h6>\n<pre class=\"lang:default decode:true\">for k, v := range myMap {\n    log.Printf(\"key=%v, value=%v\", k, v)\n}\n\nfor v := range myChannel {\n    log.Printf(\"value=%v\", v)\n}\n\nfor i, v := range myArray {\n    log.Printf(\"array value at [%d]=%v\", i, v)\n}\n\n\/\/ `range` on strings iterates over Unicode code points.\n\/\/ The first value is the starting byte index of the `rune` and the second the `rune` itself.\nfor i, c := range \"go\" {\n    fmt.Println(i, c)\n}<\/pre>\n<h6>defer<\/h6>\n<p>defer\u8bed\u53e5\u7528\u4e8e\u5ef6\u8fdf\u4e00\u4e2a\u51fd\u6570\u6216\u8005\u65b9\u6cd5(\u6216\u8005\u5f53\u524d\u6240\u521b\u5efa\u7684\u533f\u540d\u51fd\u6570)\u7684\u6267\u884c\uff0c\u5b83\u4f1a\u5728\u5916\u56f4\u51fd\u6570\u6216\u8005<strong><span style=\"color: #ff0000;\">\u65b9\u6cd5\u8fd4\u56de\u4e4b\u524d<\/span><\/strong>\u4f46\u662f\u5176<strong><span style=\"color: #ff0000;\">\u8fd4\u56de\u503c(\u5982\u679c\u6709\u7684\u8bdd)\u8ba1\u7b97\u4e4b\u540e<\/span><\/strong>\u6267\u884c\u3002<br \/>\n\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u6216\u8005\u65b9\u6cd5\u4e2d\u6709\u591a\u4e2adefer\u8bed\u53e5\uff0c\u5b83\u4eec\u4f1a\u4ee5LIFO(Last in First out)\u7684\u987a\u5e8f\u6267\u884c\u3002<\/p>\n<pre class=\"lang:default decode:true\">package main\n\nimport \"fmt\"\n\nfunc main() {\n\tdefer fmt.Println(\"world\")\n\tfmt.Println(\"hello\")\n}<\/pre>\n<h5>#\u5176\u5b83\u95ee\u9898<\/h5>\n<h6>Go\u8bed\u8a00\u7684\u6587\u4ef6\u64cd\u4f5c\uff1f<\/h6>\n<p><a href=\"http:\/\/www.devdungeon.com\/content\/working-files-go\" target=\"_blank\">http:\/\/www.devdungeon.com\/content\/working-files-go<\/a><\/p>\n<h6>Go\u8bed\u8a00\u4e2d\u5982\u4f55\u8fdb\u884c\u5b57\u7b26\u4e32\u8fde\u63a5\u64cd\u4f5c\uff1f<\/h6>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/1760757\/how-to-efficiently-concatenate-strings-in-go\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/1760757\/how-to-efficiently-concatenate-strings-in-go<\/a><\/p>\n<h6>\u5982\u4f55\u51cf\u5c0f\u7f16\u8bd1\u540e\u6587\u4ef6\u7684\u5927\u5c0f\uff1f<\/h6>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/3861634\/how-to-reduce-compiled-file-size\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/3861634\/how-to-reduce-compiled-file-size<\/a><\/p>\n<h5>#\u4e00\u4e9b\u4f8b\u5b50<\/h5>\n<pre class=\"lang:default decode:true \">\/*\n\u7b80\u5355(\u5e76\u53d1)\u7aef\u53e3\u626b\u63cf\u5668\nhttps:\/\/golang.org\/pkg\/sync\/#WaitGroup\nhttps:\/\/phpinfo.me\/2015\/06\/24\/958.html\n*\/\npackage main\n\nimport (\n\t\"fmt\"\n\t\"net\"\n\t\"os\"\n\t\"runtime\"\n\t\"sync\"\n)\n\nfunc main() {\n\truntime.GOMAXPROCS(runtime.NumCPU())\n\tif len(os.Args) != 2 {\n\t\tfmt.Fprintf(os.Stderr, \"Usage: %s ip\/host\\n\", os.Args[0])\n\t\tos.Exit(1)\n\t}\n\tip := os.Args[1]\n\tfmt.Println(\"[+]Scaning\", ip)\n\n\tports := []string{\"80\", \"443\", \"8080\", \"8009\", \"3306\", \"21\", \"22\", \"6379\", \"27017\", \"11211\", \"3389\", \"1433\"}\n\twg := sync.WaitGroup{}\n\twg.Add(len(ports))\n\tfor _, port := range ports {\n\t\tgo ScanPort(ip, port, &amp;wg)\n\t}\n\twg.Wait()\n\tfmt.Println(\"[+]Stop\", ip)\n}\n\nfunc ScanPort(ip, port string, wg *sync.WaitGroup) {\n\tsocket := ip + \":\" + port\n\t_, err := net.Dial(\"tcp\", socket)\n\tif err != nil {\n\t\tfmt.Println(\"[-]\", port, \"Close\")\n\t} else {\n\t\tfmt.Println(\"[+]\", port, \"Open\")\n\t}\n\twg.Done()\n}<\/pre>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/blog.atime.me\/note\/golang-summary.html\" target=\"_blank\">http:\/\/blog.atime.me\/note\/golang-summary.html<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/qyuhen\/book\" target=\"_blank\">\u96e8\u75d5\u5b66\u4e60\u7b14\u8bb0<\/a><\/li>\n<li><a href=\"https:\/\/golang.org\/doc\/faq\" target=\"_blank\">https:\/\/golang.org\/doc\/faq<\/a><\/li>\n<li><a href=\"https:\/\/golang.org\/ref\/spec\" target=\"_blank\">https:\/\/golang.org\/ref\/spec<\/a><\/li>\n<li><a href=\"https:\/\/golang.org\/doc\/effective_go.html\" target=\"_blank\">https:\/\/golang.org\/doc\/effective_go.html<\/a><\/li>\n<li><a href=\"http:\/\/blog.golang.org\/profiling-go-programs\" target=\"_blank\">http:\/\/blog.golang.org\/profiling-go-programs<\/a><\/li>\n<\/ul>\n<p>=EOF=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u5230\u73b0\u5728\u4e3a\u6b62\u63a5\u89e6Go\u8fd9\u95e8\u8bed\u8a00\u4e5f\u5feb2\u4e2a\u6708\u4e86\uff0c\u4f46\u8fd8\u6ca1\u7528\u8fc7Go\u5199\u8fc7\u4ec0\u4e48\u6bd4\u8f83\u5927\u7684\u7a0b\u5e8f\/\u9879\u76ee\uff08\u4e5f\u8bb8 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7,12],"tags":[563,564,19],"class_list":["post-2547","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","category-tools","tag-go","tag-golang","tag-tips"],"views":10974,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2547","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=2547"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2547\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}