{"id":221428,"date":"2021-07-06T10:00:12","date_gmt":"2021-07-06T02:00:12","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=221428"},"modified":"2021-06-25T09:57:21","modified_gmt":"2021-06-25T01:57:21","slug":"go-channel-detailed","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/go-channel-detailed.html","title":{"rendered":"Go Channel \u8be6\u7ec6\u4ecb\u7ecd"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nChannel\u662fGo\u4e2d\u7684\u4e00\u4e2a\u6838\u5fc3\u7c7b\u578b\uff0c\u4f60\u53ef\u4ee5\u628a\u5b83\u770b\u6210\u4e00\u4e2a\u7ba1\u9053\uff0c\u901a\u8fc7\u5b83\u5e76\u53d1\u6838\u5fc3\u5355\u5143\u5c31\u53ef\u4ee5\u53d1\u9001\u6216\u8005\u63a5\u6536\u6570\u636e\u8fdb\u884c\u901a\u8baf(communication)\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u5b83\u7684\u64cd\u4f5c\u7b26\u662f\u7bad\u5934 <- \u3002\n\n\n

ch <- v    \/\/ \u53d1\u9001\u503cv\u5230Channel ch\u4e2d\r\nv := <-ch  \/\/ \u4eceChannel ch\u4e2d\u63a5\u6536\u6570\u636e\uff0c\u5e76\u5c06\u6570\u636e\u8d4b\u503c\u7ed9v<\/pre>\n

(\u7bad\u5934\u7684\u6307\u5411\u5c31\u662f\u6570\u636e\u7684\u6d41\u5411)<\/p>\n

\u5c31\u50cf map \u548c slice \u6570\u636e\u7c7b\u578b\u4e00\u6837, channel\u5fc5\u987b\u5148\u521b\u5efa\u518d\u4f7f\u7528:<\/p>\n

ch := make(chan int)<\/pre>\n
Channel\u7c7b\u578b<\/strong><\/div>\n

Channel\u7c7b\u578b\u7684\u5b9a\u4e49\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n

ChannelType = ( \"chan\" | \"chan\" \"<-\" | \"<-\" \"chan\" ) ElementType .<\/pre>\n

\u5b83\u5305\u62ec\u4e09\u79cd\u7c7b\u578b\u7684\u5b9a\u4e49\u3002\u53ef\u9009\u7684<-\u4ee3\u8868channel\u7684\u65b9\u5411\u3002\u5982\u679c\u6ca1\u6709\u6307\u5b9a\u65b9\u5411\uff0c\u90a3\u4e48Channel\u5c31\u662f\u53cc\u5411\u7684\uff0c\u65e2\u53ef\u4ee5\u63a5\u6536\u6570\u636e\uff0c\u4e5f\u53ef\u4ee5\u53d1\u9001\u6570\u636e\u3002\n\n\n\n

chan T          \/\/ \u53ef\u4ee5\u63a5\u6536\u548c\u53d1\u9001\u7c7b\u578b\u4e3a T \u7684\u6570\u636e\r\nchan<- float64  \/\/ \u53ea\u53ef\u4ee5\u7528\u6765\u53d1\u9001 float64 \u7c7b\u578b\u7684\u6570\u636e\r\n<-chan int      \/\/ \u53ea\u53ef\u4ee5\u7528\u6765\u63a5\u6536 int \u7c7b\u578b\u7684\u6570\u636e<\/pre>\n

<-\u603b\u662f\u4f18\u5148\u548c\u6700\u5de6\u8fb9\u7684\u7c7b\u578b\u7ed3\u5408\u3002\n\n\n\n

chan<- chan int    \/\/ \u7b49\u4ef7 chan<- (chan int)\r\nchan<- <-chan int  \/\/ \u7b49\u4ef7 chan<- (<-chan int)\r\n<-chan <-chan int  \/\/ \u7b49\u4ef7 <-chan (<-chan int)\r\nchan (<-chan int)<\/pre>\n

\u4f7f\u7528make\u521d\u59cb\u5316Channel,\u5e76\u4e14\u53ef\u4ee5\u8bbe\u7f6e\u5bb9\u91cf:<\/p>\n

make(chan int, 100)<\/pre>\n

\u5bb9\u91cf(capacity)\u4ee3\u8868Channel\u5bb9\u7eb3\u7684\u6700\u591a\u7684\u5143\u7d20\u7684\u6570\u91cf\uff0c\u4ee3\u8868Channel\u7684\u7f13\u5b58\u7684\u5927\u5c0f\u3002
\n\u5982\u679c\u6ca1\u6709\u8bbe\u7f6e\u5bb9\u91cf\uff0c\u6216\u8005\u5bb9\u91cf\u8bbe\u7f6e\u4e3a0, \u8bf4\u660eChannel\u6ca1\u6709\u7f13\u5b58\uff0c\u53ea\u6709sender\u548creceiver\u90fd\u51c6\u5907\u597d\u4e86\u540e\u5b83\u4eec\u7684\u901a\u8baf(communication)\u624d\u4f1a\u53d1\u751f(Blocking)\u3002\u5982\u679c\u8bbe\u7f6e\u4e86\u7f13\u5b58\uff0c\u5c31\u6709\u53ef\u80fd\u4e0d\u53d1\u751f\u963b\u585e\uff0c \u53ea\u6709buffer\u6ee1\u4e86\u540e send\u624d\u4f1a\u963b\u585e\uff0c \u800c\u53ea\u6709\u7f13\u5b58\u7a7a\u4e86\u540ereceive\u624d\u4f1a\u963b\u585e\u3002\u4e00\u4e2anil channel\u4e0d\u4f1a\u901a\u4fe1\u3002<\/p>\n

\u53ef\u4ee5\u901a\u8fc7\u5185\u5efa\u7684close\u65b9\u6cd5\u53ef\u4ee5\u5173\u95edChannel\u3002<\/p>\n

\u4f60\u53ef\u4ee5\u5728\u591a\u4e2agoroutine\u4ece\/\u5f80 \u4e00\u4e2achannel \u4e2d receive\/send \u6570\u636e, \u4e0d\u5fc5\u8003\u8651\u989d\u5916\u7684\u540c\u6b65\u63aa\u65bd\u3002<\/p>\n

Channel\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u4e2a\u5148\u5165\u5148\u51fa(FIFO)\u7684\u961f\u5217\uff0c\u63a5\u6536\u7684\u6570\u636e\u548c\u53d1\u9001\u7684\u6570\u636e\u7684\u987a\u5e8f\u662f\u4e00\u81f4\u7684\u3002<\/p>\n

channel\u7684 receive\u652f\u6301 multi-valued assignment\uff0c\u5982<\/p>\n

v, ok := <-ch<\/pre>\n

send\u8bed\u53e5<\/strong>
\nsend\u8bed\u53e5\u7528\u6765\u5f80Channel\u4e2d\u53d1\u9001\u6570\u636e\uff0c \u5982ch <- 3\u3002\n\u5b83\u7684\u5b9a\u4e49\u5982\u4e0b:\n\n\n

SendStmt = Channel \"<-\" Expression .\r\nChannel  = Expression .<\/pre>\n

\u5728\u901a\u8baf(communication)\u5f00\u59cb\u524dchannel\u548cexpression\u5fc5\u9009\u5148\u6c42\u503c\u51fa\u6765(evaluated)\uff0c\u6bd4\u5982\u4e0b\u9762\u7684(3+4)\u5148\u8ba1\u7b97\u51fa7\u7136\u540e\u518d\u53d1\u9001\u7ed9channel\u3002<\/p>\n

c := make(chan int)\r\ndefer close(c)\r\ngo func() { c <- 3 + 4 }()\r\ni := <-c\r\nfmt.Println(i)<\/pre>\n

send\u88ab\u6267\u884c\u524d(proceed)\u901a\u8baf(communication)\u4e00\u76f4\u88ab\u963b\u585e\u7740\u3002\u5982\u524d\u6240\u8a00\uff0c\u65e0\u7f13\u5b58\u7684channel\u53ea\u6709\u5728receiver\u51c6\u5907\u597d\u540esend\u624d\u88ab\u6267\u884c\u3002\u5982\u679c\u6709\u7f13\u5b58\uff0c\u5e76\u4e14\u7f13\u5b58\u672a\u6ee1\uff0c\u5219send\u4f1a\u88ab\u6267\u884c\u3002<\/p>\n

\u5f80\u4e00\u4e2a\u5df2\u7ecf\u88abclose\u7684channel\u4e2d\u7ee7\u7eed\u53d1\u9001\u6570\u636e\u4f1a\u5bfc\u81f4run-time panic\u3002<\/p>\n

\u5f80nil channel\u4e2d\u53d1\u9001\u6570\u636e\u4f1a\u4e00\u81f4\u88ab\u963b\u585e\u7740\u3002<\/p>\n

receive \u64cd\u4f5c\u7b26<\/strong>
\n<-ch\u7528\u6765\u4ecechannel ch\u4e2d\u63a5\u6536\u6570\u636e\uff0c\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u4f1a\u4e00\u76f4\u88abblock,\u76f4\u5230\u6709\u6570\u636e\u53ef\u4ee5\u63a5\u6536\u3002\n\u4ece\u4e00\u4e2anil channel\u4e2d\u63a5\u6536\u6570\u636e\u4f1a\u4e00\u76f4\u88abblock\u3002\n\n\u4ece\u4e00\u4e2a\u88abclose\u7684channel\u4e2d\u63a5\u6536\u6570\u636e\u4e0d\u4f1a\u88ab\u963b\u585e\uff0c\u800c\u662f\u7acb\u5373\u8fd4\u56de\uff0c\u63a5\u6536\u5b8c\u5df2\u53d1\u9001\u7684\u6570\u636e\u540e\u4f1a\u8fd4\u56de\u5143\u7d20\u7c7b\u578b\u7684\u96f6\u503c(zero value)\u3002\n\n\u5982\u524d\u6240\u8ff0\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u989d\u5916\u7684\u8fd4\u56de\u53c2\u6570\u6765\u68c0\u67e5channel\u662f\u5426\u5173\u95ed\u3002\n\n\n\n

x, ok := <-ch\r\nx, ok = <-ch\r\nvar x, ok = <-ch<\/pre>\n

\u5982\u679cOK \u662ffalse\uff0c\u8868\u660e\u63a5\u6536\u7684x\u662f\u4ea7\u751f\u7684\u96f6\u503c\uff0c\u8fd9\u4e2achannel\u88ab\u5173\u95ed\u4e86\u6216\u8005\u4e3a\u7a7a\u3002<\/p>\n

blocking<\/strong><\/div>\n

\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u53d1\u9001\u548c\u63a5\u6536\u4f1a\u4e00\u76f4\u963b\u585e\u7740\uff0c\u76f4\u5230\u53e6\u4e00\u65b9\u51c6\u5907\u597d\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u7528\u6765\u5728gororutine\u4e2d\u8fdb\u884c\u540c\u6b65\uff0c\u800c\u4e0d\u5fc5\u4f7f\u7528\u663e\u793a\u7684\u9501\u6216\u8005\u6761\u4ef6\u53d8\u91cf\u3002<\/p>\n

\u5982\u5b98\u65b9\u7684\u4f8b\u5b50\u4e2dx, y := <-c, <-c\u8fd9\u53e5\u4f1a\u4e00\u76f4\u7b49\u5f85\u8ba1\u7b97\u7ed3\u679c\u53d1\u9001\u5230channel\u4e2d\u3002\n\n\n\n

import \"fmt\"\r\nfunc sum(s []int, c chan int) {\r\n    sum := 0\r\n    for _, v := range s {\r\n        sum += v\r\n    }\r\n    c <- sum \/\/ send sum to c\r\n}\r\nfunc main() {\r\n    s := []int{7, 2, 8, -9, 4, 0}\r\n    c := make(chan int)\r\n    go sum(s[:len(s)\/2], c)\r\n    go sum(s[len(s)\/2:], c)\r\n    x, y := <-c, <-c \/\/ receive from c\r\n    fmt.Println(x, y, x+y)\r\n}<\/pre>\n
Buffered Channels<\/strong><\/div>\n

make\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6307\u5b9a\u7f13\u5b58\u7684\u5927\u5c0f\uff1ach := make(chan int, 100)\u3002<\/p>\n

\u901a\u8fc7\u7f13\u5b58\u7684\u4f7f\u7528\uff0c\u53ef\u4ee5\u5c3d\u91cf\u907f\u514d\u963b\u585e\uff0c\u63d0\u4f9b\u5e94\u7528\u7684\u6027\u80fd\u3002<\/p>\n

Range<\/strong><\/div>\n
for \u2026\u2026 range\u8bed\u53e5\u53ef\u4ee5\u5904\u7406Channel\u3002\r\n\r\nfunc main() {\r\n    go func() {\r\n        time.Sleep(1 * time.Hour)\r\n    }()\r\n    c := make(chan int)\r\n    go func() {\r\n        for i := 0; i < 10; i = i + 1 {\r\n            c <- i\r\n        }\r\n        close(c)\r\n    }()\r\n    for i := range c {\r\n        fmt.Println(i)\r\n    }\r\n    fmt.Println(\"Finished\")\r\n}<\/pre>\n

range c\u4ea7\u751f\u7684\u8fed\u4ee3\u503c\u4e3aChannel\u4e2d\u53d1\u9001\u7684\u503c\uff0c\u5b83\u4f1a\u4e00\u76f4\u8fed\u4ee3\u76f4\u5230channel\u88ab\u5173\u95ed\u3002\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u5982\u679c\u628aclose(c)\u6ce8\u91ca\u6389\uff0c\u7a0b\u5e8f\u4f1a\u4e00\u76f4\u963b\u585e\u5728for \u2026\u2026 range\u90a3\u4e00\u884c\u3002<\/p>\n

select<\/strong><\/div>\n

select\u8bed\u53e5\u9009\u62e9\u4e00\u7ec4\u53ef\u80fd\u7684send\u64cd\u4f5c\u548creceive\u64cd\u4f5c\u53bb\u5904\u7406\u3002\u5b83\u7c7b\u4f3cswitch,\u4f46\u662f\u53ea\u662f\u7528\u6765\u5904\u7406\u901a\u8baf(communication)\u64cd\u4f5c\u3002
\n\u5b83\u7684case\u53ef\u4ee5\u662fsend\u8bed\u53e5\uff0c\u4e5f\u53ef\u4ee5\u662freceive\u8bed\u53e5\uff0c\u4ea6\u6216\u8005default\u3002<\/p>\n

receive\u8bed\u53e5\u53ef\u4ee5\u5c06\u503c\u8d4b\u503c\u7ed9\u4e00\u4e2a\u6216\u8005\u4e24\u4e2a\u53d8\u91cf\u3002\u5b83\u5fc5\u987b\u662f\u4e00\u4e2areceive\u64cd\u4f5c\u3002<\/p>\n

\u6700\u591a\u5141\u8bb8\u6709\u4e00\u4e2adefault case,\u5b83\u53ef\u4ee5\u653e\u5728case\u5217\u8868\u7684\u4efb\u4f55\u4f4d\u7f6e\uff0c\u5c3d\u7ba1\u6211\u4eec\u5927\u90e8\u5206\u4f1a\u5c06\u5b83\u653e\u5728\u6700\u540e\u3002<\/p>\n

import \"fmt\"\r\nfunc fibonacci(c, quit chan int) {\r\n    x, y := 0, 1\r\n    for {\r\n        select {\r\n        case c <- x:\r\n            x, y = y, x+y\r\n        case <-quit:\r\n            fmt.Println(\"quit\")\r\n            return\r\n        }\r\n    }\r\n}\r\nfunc main() {\r\n    c := make(chan int)\r\n    quit := make(chan int)\r\n    go func() {\r\n        for i := 0; i < 10; i++ {\r\n            fmt.Println(<-c)\r\n        }\r\n        quit <- 0\r\n    }()\r\n    fibonacci(c, quit)\r\n}<\/pre>\n

\u5982\u679c\u6709\u540c\u65f6\u591a\u4e2acase\u53bb\u5904\u7406,\u6bd4\u5982\u540c\u65f6\u6709\u591a\u4e2achannel\u53ef\u4ee5\u63a5\u6536\u6570\u636e\uff0c\u90a3\u4e48Go\u4f1a\u4f2a\u968f\u673a\u7684\u9009\u62e9\u4e00\u4e2acase\u5904\u7406(pseudo-random)\u3002\u5982\u679c\u6ca1\u6709case\u9700\u8981\u5904\u7406\uff0c\u5219\u4f1a\u9009\u62e9default\u53bb\u5904\u7406\uff0c\u5982\u679cdefault case\u5b58\u5728\u7684\u60c5\u51b5\u4e0b\u3002\u5982\u679c\u6ca1\u6709default case\uff0c\u5219select\u8bed\u53e5\u4f1a\u963b\u585e\uff0c\u76f4\u5230\u67d0\u4e2acase\u9700\u8981\u5904\u7406\u3002<\/p>\n

\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cnil channel\u4e0a\u7684\u64cd\u4f5c\u4f1a\u4e00\u76f4\u88ab\u963b\u585e\uff0c\u5982\u679c\u6ca1\u6709default case,\u53ea\u6709nil channel\u7684select\u4f1a\u4e00\u76f4\u88ab\u963b\u585e\u3002<\/p>\n

select\u8bed\u53e5\u548cswitch\u8bed\u53e5\u4e00\u6837\uff0c\u5b83\u4e0d\u662f\u5faa\u73af\uff0c\u5b83\u53ea\u4f1a\u9009\u62e9\u4e00\u4e2acase\u6765\u5904\u7406\uff0c\u5982\u679c\u60f3\u4e00\u76f4\u5904\u7406channel\uff0c\u4f60\u53ef\u4ee5\u5728\u5916\u9762\u52a0\u4e00\u4e2a\u65e0\u9650\u7684for\u5faa\u73af\uff1a<\/p>\n

for {\r\n    select {\r\n    case c <- x:\r\n        x, y = y, x+y\r\n    case <-quit:\r\n        fmt.Println(\"quit\")\r\n        return\r\n    }\r\n}<\/pre>\n
timeout<\/strong><\/div>\n

select\u6709\u5f88\u91cd\u8981\u7684\u4e00\u4e2a\u5e94\u7528\u5c31\u662f\u8d85\u65f6\u5904\u7406\u3002 \u56e0\u4e3a\u4e0a\u9762\u6211\u4eec\u63d0\u5230\uff0c\u5982\u679c\u6ca1\u6709case\u9700\u8981\u5904\u7406\uff0cselect\u8bed\u53e5\u5c31\u4f1a\u4e00\u76f4\u963b\u585e\u7740\u3002\u8fd9\u65f6\u5019\u6211\u4eec\u53ef\u80fd\u5c31\u9700\u8981\u4e00\u4e2a\u8d85\u65f6\u64cd\u4f5c\uff0c\u7528\u6765\u5904\u7406\u8d85\u65f6\u7684\u60c5\u51b5\u3002
\n\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u6211\u4eec\u4f1a\u57282\u79d2\u540e\u5f80channel c1\u4e2d\u53d1\u9001\u4e00\u4e2a\u6570\u636e\uff0c\u4f46\u662fselect\u8bbe\u7f6e\u4e3a1\u79d2\u8d85\u65f6,\u56e0\u6b64\u6211\u4eec\u4f1a\u6253\u5370\u51fatimeout 1,\u800c\u4e0d\u662fresult 1\u3002<\/p>\n

import \"time\"\r\nimport \"fmt\"\r\nfunc main() {\r\n    c1 := make(chan string, 1)\r\n    go func() {\r\n        time.Sleep(time.Second * 2)\r\n        c1 <- \"result 1\"\r\n    }()\r\n    select {\r\n    case res := <-c1:\r\n        fmt.Println(res)\r\n    case <-time.After(time.Second * 1):\r\n        fmt.Println(\"timeout 1\")\r\n    }\r\n}<\/pre>\n

\u5176\u5b9e\u5b83\u5229\u7528\u7684\u662ftime.After\u65b9\u6cd5\uff0c\u5b83\u8fd4\u56de\u4e00\u4e2a\u7c7b\u578b\u4e3a<-chan Time\u7684\u5355\u5411\u7684channel\uff0c\u5728\u6307\u5b9a\u7684\u65f6\u95f4\u53d1\u9001\u4e00\u4e2a\u5f53\u524d\u65f6\u95f4\u7ed9\u8fd4\u56de\u7684channel\u4e2d\u3002\n\n[yiji]Timer\u548cTicker[\/yiji]\n\u6211\u4eec\u770b\u4e00\u4e0b\u5173\u4e8e\u65f6\u95f4\u7684\u4e24\u4e2aChannel\u3002\ntimer\u662f\u4e00\u4e2a\u5b9a\u65f6\u5668\uff0c\u4ee3\u8868\u672a\u6765\u7684\u4e00\u4e2a\u5355\u4e00\u4e8b\u4ef6\uff0c\u4f60\u53ef\u4ee5\u544a\u8bc9timer\u4f60\u8981\u7b49\u5f85\u591a\u957f\u65f6\u95f4\uff0c\u5b83\u63d0\u4f9b\u4e00\u4e2aChannel\uff0c\u5728\u5c06\u6765\u7684\u90a3\u4e2a\u65f6\u95f4\u90a3\u4e2aChannel\u63d0\u4f9b\u4e86\u4e00\u4e2a\u65f6\u95f4\u503c\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u7b2c\u4e8c\u884c\u4f1a\u963b\u585e2\u79d2\u949f\u5de6\u53f3\u7684\u65f6\u95f4\uff0c\u76f4\u5230\u65f6\u95f4\u5230\u4e86\u624d\u4f1a\u7ee7\u7eed\u6267\u884c\u3002\n\n\n\n

timer1 := time.NewTimer(time.Second * 2)\r\n<-timer1.C\r\nfmt.Println(\"Timer 1 expired\")<\/pre>\n

\u5f53\u7136\u5982\u679c\u4f60\u53ea\u662f\u60f3\u5355\u7eaf\u7684\u7b49\u5f85\u7684\u8bdd\uff0c\u53ef\u4ee5\u4f7f\u7528time.Sleep\u6765\u5b9e\u73b0\u3002<\/p>\n

\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528timer.Stop\u6765\u505c\u6b62\u8ba1\u65f6\u5668\u3002<\/p>\n

timer2 := time.NewTimer(time.Second)\r\ngo func() {\r\n    <-timer2.C\r\n    fmt.Println(\"Timer 2 expired\")\r\n}()\r\nstop2 := timer2.Stop()\r\nif stop2 {\r\n    fmt.Println(\"Timer 2 stopped\")\r\n}<\/pre>\n

ticker\u662f\u4e00\u4e2a\u5b9a\u65f6\u89e6\u53d1\u7684\u8ba1\u65f6\u5668\uff0c\u5b83\u4f1a\u4ee5\u4e00\u4e2a\u95f4\u9694(interval)\u5f80Channel\u53d1\u9001\u4e00\u4e2a\u4e8b\u4ef6(\u5f53\u524d\u65f6\u95f4)\uff0c\u800cChannel\u7684\u63a5\u6536\u8005\u53ef\u4ee5\u4ee5\u56fa\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\u4eceChannel\u4e2d\u8bfb\u53d6\u4e8b\u4ef6\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2dticker\u6bcf500\u6beb\u79d2\u89e6\u53d1\u4e00\u6b21\uff0c\u4f60\u53ef\u4ee5\u89c2\u5bdf\u8f93\u51fa\u7684\u65f6\u95f4\u3002<\/p>\n

ticker := time.NewTicker(time.Millisecond * 500)\r\ngo func() {\r\n    for t := range ticker.C {\r\n        fmt.Println(\"Tick at\", t)\r\n    }\r\n}()<\/pre>\n

\u7c7b\u4f3ctimer, ticker\u4e5f\u53ef\u4ee5\u901a\u8fc7Stop\u65b9\u6cd5\u6765\u505c\u6b62\u3002\u4e00\u65e6\u5b83\u505c\u6b62\uff0c\u63a5\u6536\u8005\u4e0d\u518d\u4f1a\u4ecechannel\u4e2d\u63a5\u6536\u6570\u636e\u4e86\u3002<\/p>\n

close<\/strong><\/div>\n

\u5185\u5efa\u7684close\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u5173\u95edchannel\u3002<\/p>\n

\u603b\u7ed3\u4e00\u4e0bchannel\u5173\u95ed\u540esender\u7684receiver\u64cd\u4f5c\u3002
\n\u5982\u679cchannel c\u5df2\u7ecf\u88ab\u5173\u95ed,\u7ee7\u7eed\u5f80\u5b83\u53d1\u9001\u6570\u636e\u4f1a\u5bfc\u81f4panic: send on closed channel:<\/p>\n

import \"time\"\r\nfunc main() {\r\n    go func() {\r\n        time.Sleep(time.Hour)\r\n    }()\r\n    c := make(chan int, 10)\r\n    c <- 1\r\n    c <- 2\r\n    close(c)\r\n    c <- 3\r\n}<\/pre>\n

\u4f46\u662f\u4ece\u8fd9\u4e2a\u5173\u95ed\u7684channel\u4e2d\u4e0d\u4f46\u53ef\u4ee5\u8bfb\u53d6\u51fa\u5df2\u53d1\u9001\u7684\u6570\u636e\uff0c\u8fd8\u53ef\u4ee5\u4e0d\u65ad\u7684\u8bfb\u53d6\u96f6\u503c:<\/p>\n

c := make(chan int, 10)\r\nc <- 1\r\nc <- 2\r\nclose(c)\r\nfmt.Println(<-c) \/\/1\r\nfmt.Println(<-c) \/\/2\r\nfmt.Println(<-c) \/\/0\r\nfmt.Println(<-c) \/\/0<\/pre>\n

\u4f46\u662f\u5982\u679c\u901a\u8fc7range\u8bfb\u53d6\uff0cchannel\u5173\u95ed\u540efor\u5faa\u73af\u4f1a\u8df3\u51fa\uff1a<\/p>\n

c := make(chan int, 10)\r\nc <- 1\r\nc <- 2\r\nclose(c)\r\nfor i := range c {\r\n    fmt.Println(i)\r\n}<\/pre>\n

\u901a\u8fc7i, ok := <-c\u53ef\u4ee5\u67e5\u770bChannel\u7684\u72b6\u6001\uff0c\u5224\u65ad\u503c\u662f\u96f6\u503c\u8fd8\u662f\u6b63\u5e38\u8bfb\u53d6\u7684\u503c\u3002\n\n\n\n

c := make(chan int, 10)\r\nclose(c)\r\ni, ok := <-c\r\nfmt.Printf(\"%d, %t\", i, ok) \/\/0, false<\/pre>\n
\u540c\u6b65<\/strong><\/div>\n

channel\u53ef\u4ee5\u7528\u5728goroutine\u4e4b\u95f4\u7684\u540c\u6b65\u3002
\n\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2dmain goroutine\u901a\u8fc7done channel\u7b49\u5f85worker\u5b8c\u6210\u4efb\u52a1\u3002 worker\u505a\u5b8c\u4efb\u52a1\u540e\u53ea\u9700\u5f80channel\u53d1\u9001\u4e00\u4e2a\u6570\u636e\u5c31\u53ef\u4ee5\u901a\u77e5main goroutine\u4efb\u52a1\u5b8c\u6210\u3002<\/p>\n

import (\r\n    \"fmt\"\r\n    \"time\"\r\n)\r\nfunc worker(done chan bool) {\r\n    time.Sleep(time.Second)\r\n    \/\/ \u901a\u77e5\u4efb\u52a1\u5df2\u5b8c\u6210\r\n    done <- true\r\n}\r\nfunc main() {\r\n    done := make(chan bool, 1)\r\n    go worker(done)\r\n    \/\/ \u7b49\u5f85\u4efb\u52a1\u5b8c\u6210\r\n    <-done\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u5b83\u7684\u64cd\u4f5c\u7b26\u662f\u7bad\u5934<\/p>\n","protected":false},"author":321,"featured_media":221431,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-221428","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/221428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=221428"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/221428\/revisions"}],"predecessor-version":[{"id":221438,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/221428\/revisions\/221438"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/221431"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=221428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=221428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=221428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}