{"id":2954,"date":"2019-03-12T16:11:41","date_gmt":"2019-03-12T08:11:41","guid":{"rendered":"https:\/\/damogame.cn:15443\/wordpress\/?p=2954"},"modified":"2019-03-12T16:11:41","modified_gmt":"2019-03-12T08:11:41","slug":"go-%e8%af%ad%e8%a8%80%e4%b8%ad%e4%bd%bf%e7%94%a8-future-promise-%e7%bc%96%e7%a8%8b%e6%a8%a1%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/2954","title":{"rendered":"Go \u8bed\u8a00\u4e2d\u4f7f\u7528 Future\/Promise \u7f16\u7a0b\u6a21\u5f0f"},"content":{"rendered":"<div class=\"author\">\n<div class=\"info\">\u73b0\u5982\u4eca\uff0c\u5e94\u7528\u6267\u884c\u65f6\u6700\u666e\u904d\u5b58\u5728\u7684\u74f6\u9888\u5c31\u662f\u7f51\u7edc\u8bf7\u6c42\u4e86\u3002\u7f51\u7edc\u8bf7\u6c42\u53ea\u8981\u51e0\u6beb\u79d2\uff0c\u4f46\u662f\u7b49\u5230\u8fd4\u56de\u5374\u8981\u767e\u500d\u7684\u65f6\u95f4\u3002\u6240\u4ee5\uff0c\u5982\u679c\u4f60\u6267\u884c\u591a\u4e2a\u7f51\u7edc\u8bf7\u6c42\uff0c\u8ba9\u4ed6\u4eec\u90fd\u5e76\u884c\u6267\u884c\u5c31\u662f\u51cf\u5c11\u5ef6\u8fdf\u6700\u597d\u7684\u9009\u62e9\u4e86\u3002<a href=\"https:\/\/link.jianshu.com\/?t=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FFutures_and_promises\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Future\/Promise<\/a>\u00a0\u5c31\u662f\u5b9e\u73b0\u8fd9\u4e00\u76ee\u7684\u7684\u624b\u6bb5\u4e4b\u4e00\u3002<\/div>\n<\/div>\n<div class=\"show-content\" data-note-content=\"\">\n<div class=\"show-content-free\">\n<p>\u4e00\u4e2a Future \u5c31\u662f\u8bf4\u201c\u5c06\u6765\u201d\u4f60\u9700\u8981\u67d0\u4e9b\u4e1c\u897f\uff08\u4e00\u822c\u5c31\u662f\u4e00\u4e2a\u7f51\u7edc\u8bf7\u6c42\u7684\u7ed3\u679c\uff09\uff0c\u4f46\u662f\u4f60\u73b0\u5728\u5c31\u8981\u53d1\u8d77\u8fd9\u6837\u7684\u8bf7\u6c42\uff0c\u5e76\u4e14\u8fd9\u4e2a\u8bf7\u6c42\u4f1a\u5f02\u6b65\u6267\u884c\u3002\u6216\u8005\u6362\u4e00\u4e2a\u8bf4\u6cd5\uff0c\u4f60\u9700\u8981\u5728\u540e\u53f0\u6267\u884c\u4e00\u4e2a\u5f02\u6b65\u8bf7\u6c42\u3002<\/p>\n<p>Future\/Promise \u6a21\u5f0f\u5728\u591a\u79cd\u8bed\u8a00\u90fd\u6709\u5bf9\u5e94\u7684\u5b9e\u73b0\u3002\u6700\u5178\u578b\u7684 C++11 \u6807\u51c6\u5e93\u4e2d\u5c31\u63d0\u4f9b\u4e86 future\/promise\uff0c\u53e6\u5916\uff0c\u518d\u6bd4\u5982 ES2015 \u5c31\u6709 Promise \u548c async-await\uff0cScala \u4e5f\u5185\u7f6e\u4e86 Future\u3002<\/p>\n<p>\u6700\u540e\uff0c\u867d\u7136 Go \u8bed\u8a00\u6ca1\u6709\u63d0\u4f9b future\/promise \u539f\u8bed\uff0c\u4f46\u662f\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 goroutine \u548c channel \u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\u3002\u4e0b\u9762\u7ed9\u51fa\u4e00\u4e2a\u7b80\u5355\u7684\u5b9e\u73b0\u3002<\/p>\n<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/\/ RequestFuture, http request promise.\r\nfunc RequestFuture(url string) &lt;-chan []byte {\r\n    c := make(chan []byte, 1)\r\n    go func() {\r\n        var body []byte\r\n        defer func() {\r\n            c &lt;- body\r\n        }()\r\n\r\n        res, err := http.Get(url)\r\n        if err != nil {\r\n            return\r\n        }\r\n        defer res.Body.Close()\r\n\r\n        body, _ = ioutil.ReadAll(res.Body)\r\n    }()\r\n\r\n    return c\r\n}\r\n\r\nfunc main() {\r\n    future := RequestFuture(\"https:\/\/api.github.com\/users\/octocat\/orgs\")\r\n    body := &lt;-future\r\n    log.Printf(\"reponse length: %d\", len(body))\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<div class=\"show-content-free\">\n<p><code>RequestFuture<\/code>\u00a0\u65b9\u6cd5\u7acb\u523b\u8fd4\u56de\u4e00\u4e2achannel\uff0c\u8fd9\u4e2a\u65f6\u5019 http \u8bf7\u6c42\u8fd8\u5728\u4e00\u4e2a goroutine \u540e\u53f0\u5f02\u6b65\u8fd0\u884c\u3002main \u65b9\u6cd5\u53ef\u4ee5\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u7684\u4ee3\u7801\uff0c\u6bd4\u5982\u89e6\u53d1\u5176\u4ed6\u7684 Future \u7b49\u3002\u5f53\u9700\u8981\u7ed3\u679c\u7684\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u4ece channel \u91cc\u8bfb\u53d6\u7ed3\u679c\u3002\u5982\u679c http \u8bf7\u6c42\u8fd8\u6ca1\u6709\u8fd4\u56de\u7684\u8bdd\u5c31\u4f1a\u963b\u585e\u5f53\u524d\u7684 goroutine\uff0c\u76f4\u5230\u7ed3\u679c\u8fd4\u56de\u3002<\/p>\n<p>\u7136\u800c\uff0c\u4ee5\u4e0a\u7684\u65b9\u6cd5\u8fd8\u6709\u4e00\u70b9\u5c40\u9650\u3002\u9519\u8bef\u65e0\u6cd5\u8fd4\u56de\u3002\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u91cc\uff0c\u5982\u679c http \u8bf7\u6c42\u51fa\u73b0\u9519\u8bef\u7684\u8bdd\uff0cbody \u7684\u503c\u4f1a\u662f nil\/empty\u3002\u4f46\u662f\uff0c\u7531\u4e8e channel \u53ea\u80fd\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u4f60\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5355\u72ec\u7684 struct \u6765\u5305\u88c5\u4e24\u4e2a\u8fd4\u56de\u7684\u7ed3\u679c\u3002<\/p>\n<p>\u4fee\u6539\u4ee5\u540e\u7684\u7ed3\u679c\uff1a<\/p>\n<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/\/ RequestFutureV2 return value and error\r\nfunc RequestFutureV2(url string) func() ([]byte, error) {\r\n    var body []byte\r\n    var err error\r\n\r\n    c := make(chan struct{}, 1)\r\n    go func() {\r\n        defer close(c)\r\n\r\n        var res *http.Response\r\n        res, err = http.Get(url)\r\n        if err != nil {\r\n            return\r\n        }\r\n\r\n        defer res.Body.Close()\r\n        body, err = ioutil.ReadAll(res.Body)\r\n    }()\r\n\r\n    return func() ([]byte, error) {\r\n        &lt;-c\r\n        return body, err\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<div class=\"show-content-free\">\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u56de\u4e86\u4e24\u4e2a\u7ed3\u679c\uff0c\u89e3\u51b3\u4e86\u7b2c\u4e00\u4e2a\u65b9\u6cd5\u7684\u5c40\u9650\u6027\u95ee\u9898\u3002\u4f7f\u7528\u7684\u65f6\u5019\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">func main() {\r\n    futureV2 := RequestFutureV2(\"https:\/\/api.github.com\/users\/octocat\/orgs\")\r\n\r\n    \/\/ not block\r\n    log.Printf(\"V2 is this locked again\")\r\n\r\n    bodyV2, err := futureV2() \/\/ block\r\n    if err == nil {\r\n        log.Printf(\"V2 response length %d\\n\", len(bodyV2))\r\n    } else {\r\n        log.Printf(\"V2 error is %v\\n\", err)\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<div class=\"show-content-free\">\n<p>\u4e0a\u9762\u7684\u4fee\u6539\u5e26\u6765\u7684\u597d\u5904\u5c31\u662f\u00a0<code>futureV2()<\/code>\u00a0\u65b9\u6cd5\u7684\u8c03\u7528\u53ef\u4ee5\u662f\u591a\u6b21\u7684\u3002\u5e76\u4e14\u90fd\u53ef\u4ee5\u8fd4\u56de\u540c\u6837\u7684\u7ed3\u679c\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u5982\u679c\u4f60\u60f3\u7528\u8fd9\u4e2a\u65b9\u6cd5\u5b9e\u73b0\u5f88\u591a\u4e0d\u540c\u7684\u5f02\u6b65\u529f\u80fd\uff0c\u4f60\u9700\u8981\u5199\u5f88\u591a\u7684\u989d\u5916\u7684\u4ee3\u7801\u3002\u6211\u4eec\u53ef\u4ee5\u5199\u4e00\u4e2autil \u65b9\u6cd5\u6765\u514b\u670d\u8fd9\u4e2a\u56f0\u96be\u3002<\/p>\n<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/\/ Future boilerplate method\r\nfunc Future(f func() (interface{}, error)) func() (interface{}, error) {\r\n    var result interface{}\r\n    var err error\r\n\r\n    c := make(chan struct{}, 1)\r\n    go func() {\r\n        defer close(c)\r\n        result, err = f()\r\n    }()\r\n\r\n    return func() (interface{}, error) {\r\n        &lt;-c\r\n        return result, err\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<div class=\"show-content-free\">\n<p>\u8c03\u7528\u00a0<code>Future<\/code>\u00a0\u65b9\u6cd5\u7684\u65f6\u5019\u4f1a\u6267\u884c\u623f\u91cc\u7684\u5f88\u591a channel \u65b9\u9762\u7684\u5c0f\u6280\u5de7\u3002\u4e3a\u4e86\u80fd\u591f\u8fbe\u5230\u901a\u7528\u7684\u76ee\u7684\uff0c\u8fd9\u91cc\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u4ece\u00a0<code>[]byte<\/code>-&gt;<code>interface{}<\/code>-&gt;<code>[]byte<\/code>\u00a0\u7684\u7c7b\u578b\u8f6c\u6362\u3002\u5982\u679c\u51fa\u73b0\u9519\u7684\u8bdd\u4f1a\u5f15\u53d1\u4e00\u4e2a\u8fd0\u884c\u65f6\u7684 panic\u3002<\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u73b0\u5982\u4eca\uff0c\u5e94\u7528\u6267\u884c\u65f6\u6700\u666e\u904d\u5b58\u5728\u7684\u74f6\u9888\u5c31\u662f\u7f51<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[122],"class_list":["post-2954","post","type-post","status-publish","format-standard","hentry","tag-06-godocker"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/2954","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=2954"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/2954\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=2954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=2954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=2954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}