{"id":41296,"date":"2024-12-01T10:31:00","date_gmt":"2024-12-01T02:31:00","guid":{"rendered":"https:\/\/fwq.ai\/blog\/41296\/"},"modified":"2024-12-01T10:31:00","modified_gmt":"2024-12-01T02:31:00","slug":"golang%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bb%8e%e8%be%83%e5%b0%8f%e7%9a%84%e5%ad%97%e8%8a%82%e6%95%b0%e7%bb%84%e5%a1%ab%e5%85%85%e8%be%83%e5%a4%a7%e7%9a%84%e5%ad%97%e8%8a%82%e6%95%b0%e7%bb%84%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/41296\/","title":{"rendered":"golang\u4e2d\u5982\u4f55\u4ece\u8f83\u5c0f\u7684\u5b57\u8282\u6570\u7ec4\u586b\u5145\u8f83\u5927\u7684\u5b57\u8282\u6570\u7ec4\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>golang\u4e2d\u5982\u4f55\u4ece\u8f83\u5c0f\u7684\u5b57\u8282\u6570\u7ec4\u586b\u5145\u8f83\u5927\u7684\u5b57\u8282\u6570\u7ec4\uff1f<\/span><\/p>\n<h1>golang\u4e2d\u5982\u4f55\u4ece\u8f83\u5c0f\u7684\u5b57\u8282\u6570\u7ec4\u586b\u5145\u8f83\u5927\u7684\u5b57\u8282\u6570\u7ec4\uff1f<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-19 18:45:36<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300agolang\u4e2d\u5982\u4f55\u4ece\u8f83\u5c0f\u7684\u5b57\u8282\u6570\u7ec4\u586b\u5145\u8f83\u5927\u7684\u5b57\u8282\u6570\u7ec4\uff1f\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u56e0\u6b64\uff0c\u6211\u6b63\u5728\u4ece url \u4e0b\u8f7d\u4e00\u4e2a\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u62c6\u5206\u4e3a\u5e26\u6709 <code>content-range<\/code> \u6807\u5934\u7684\u5757\uff0c\u5e76\u4e14\u5e0c\u671b\u5c06\u4e0b\u8f7d\u6587\u4ef6\u6d41\u5f0f\u4f20\u8f93\u5230\u8fdb\u5ea6\u680f\u3002\u76ee\u524d\uff0c\u6211\u5728\u5c06\u4e0b\u8f7d\u7684\u5b57\u8282\u5199\u5165\u5757\u65f6\u9047\u5230\u4e86\u56f0\u96be\u3002\u6211\u7684\u505a\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>\ntype Chunk struct {\n    response _http.Response\n    wg       *sync.WaitGroup\n    index    int\n    start    int64\n    end      int64\n    size     int64\n    data     []byte\n    ctx      context.Context\n}\n\nfunc New(ctx context.Context, res _http.Response, index int, wg *sync.WaitGroup) *Chunk {\n    totalpart := int64(res.Totalpart)\n    partsize := res.Size \/ totalpart\n\n    start := int64(index * int(partsize))\n    end := start + int64(int(partsize)-1)\n\n    if index == int(totalpart)-1 {\n        end = res.Size\n    }\n\n    return &amp;Chunk{\n        response: res,\n        wg:       wg,\n        index:    index,\n        start:    start,\n        end:      end,\n        size:     partsize,\n        data:     make([]byte, partsize),\n        ctx:      ctx,\n    }\n}\n\nfunc (c *Chunk) download() error {\n    defer c.wg.Done()\n\n    http_ := &amp;http.Client{}\n    part := fmt.Sprintf(\"bytes=%d-%d\", c.start, c.end)\n\n    if c.size == -1 {\n        log.Printf(\"Downloading chunk %d with size unknown\", c.index+1)\n    } else {\n        log.Printf(\"Downloading chunk %d from %d to %d (~%d MB)\", c.index+1, c.start, c.end, (len(c.data))\/(1024*1024))\n    }\n\n    req, err := http.NewRequest(\"GET\", c.response.Url, nil)\n    if err != nil {\n        return err\n    }\n\n    start := time.Now()\n\n    req.Header.Add(\"Range\", part)\n    res, err := http_.Do(req)\n    if err != nil {\n        return err\n    }\n\n    defer res.Body.Close()\n\n    var _100KB int64 = 1024 * 100\n    buffer := make([]byte, _100KB)\n\n    for {\n        n, err := io.ReadFull(res.Body, buffer)\n        if err == io.EOF {\n            break\n        }\n\n        \/\/TODO: combine the downloaded bytes into c.data\n        c.data = append(c.data, buffer[:n]...)\n        runtime.EventsEmit(c.ctx, \"transfered\", c.index, n)\n    }\n\n\n    \/\/ c.data, err = io.ReadAll(res.Body)\n    \/\/ if err != nil {\n    \/\/  return err\n    \/\/ }\n\n    \/\/ runtime.EventsEmit(c.ctx, \"transfered\", len(c.data))\n\n    elapsed := time.Since(start)\n    log.Printf(\"Chunk %d downloaded in %v s\\n\", c.index+1, elapsed.Seconds())\n\n    return nil\n}\n<\/pre>\n<p>\u800c <code>_http.response<\/code> \u662f\u6211\u7684\u81ea\u5b9a\u4e49\u7ed3\u6784\uff0c\u7528\u4e8e\u5728\u4e0b\u8f7d\u4e4b\u524d\u83b7\u53d6 url \u7684\u6570\u636e\uff08\u4f8b\u5982\u6587\u4ef6\u603b\u5927\u5c0f\u3001\u53ef\u62c6\u5206\uff1f\u3001\u603b\u90e8\u5206\u548c\u5185\u5bb9\u7c7b\u578b\uff09\u3002<\/p>\n<p>\u4f46\u662f\u4e0a\u9762\u7684\u4ee3\u7801\u6709\u4e00\u4e2a\u95ee\u9898\uff0c\u5373<code>c.data<\/code>\uff08\u5757\u6570\u636e\uff09\u7684<code>len()<\/code>\u53d8\u5f97\u6bd4\u5b9e\u9645\u5757\u5927\u5c0f\u5927\u3002\u4f8b\u5982\uff0c\u5b9e\u9645\u89c6\u9891\u6587\u4ef6\u7684\u5757\u5927\u5c0f\u662f 12 mb\uff0c\u4f46\u662f\u4f7f\u7528\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u5757\u53d8\u5f97\u4e24\u500d\u5927\uff0c\u5f53\u7136\uff0c\u5f53\u6240\u6709\u5757\u5408\u5e76\u5230\u4e00\u4e2a\u89c6\u9891\u6587\u4ef6\u4e2d\u65f6\uff0c\u800c\u4e0d\u662f\u91c7\u7528 48 mb\uff08\u4f8b\u5982\uff09 \uff0c\u53d8\u6210\u4e86100mb\uff0c\u4e0b\u8f7d\u7684\u89c6\u9891\u65e0\u6cd5\u64ad\u653e\u3002<\/p>\n<p>\u5728\u6211\u4e4b\u524d\u7684\u5c1d\u8bd5\u4e2d\uff0c\u6211\u4f7f\u7528\u4e86 <code>io.readall(res.body)<\/code> \uff08\u6211\u6ce8\u91ca\u7684\u4ee3\u7801\uff09\uff0c\u7136\u540e\u5c06\u5176\u5199\u5165 <code>c.data<\/code> \u4e2d\u3002\u4e0b\u8f7d\u5b8c\u6210\u540e\uff0c\u89c6\u9891\u6587\u4ef6\u53ef\u4ee5\u64ad\u653e\uff0c\u4f46\u662f\u7528\u4e0a\u9762\u7684\u65b9\u6cd5\u5374\u4e0d\u80fd\u64ad\u653e\u3002\u7f3a\u70b9\u662f\uff0c\u6211\u65e0\u6cd5\u6d41\u5f0f\u4f20\u8f93\u4e0b\u8f7d\uff0c\u56e0\u4e3a\u6211\u9700\u8981\u5b8c\u6574\u4e0b\u8f7d<\/p>\n<p>\u5982\u679c\u6211\u8981\u5728\u4e0b\u8f7d\u5757\u4e4b\u524d\u548c\u4e4b\u540e\u8bb0\u5f55\u6587\u4ef6\u7684 <code>len()<\/code>\uff0c\u5219\u4e0b\u8f7d\u8fc7\u7a0b\u4e4b\u540e\u7684 <code>len()<\/code> \u5c06\u4f1a\u53d8\u5927 2 \u500d\u3002<\/p>\n<p>\u6211\u8ba4\u4e3a\u5e26\u6709\u9644\u52a0\u7684\u6269\u5c55\u8fd0\u7b97\u7b26\u8db3\u4ee5\u4ece\u5934\u5230\u5c3e\u586b\u5145 <code>c.data<\/code> \u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u6211\u8ba4\u4e3a\uff0c\u56e0\u4e3a\u5b83\u4f7f\u7528\u7684\u662f <code>append<\/code>\uff0c\u6240\u4ee5\u6570\u636e\u662f\u5426\u8d85\u8fc7\u6570\u7ec4\u542f\u52a8\u65f6\u7684 <code>len()<\/code> \u5e76\u4e0d\u91cd\u8981\u3002\u6709\u4eba\u77e5\u9053\u8fd9\u4e2a\u95ee\u9898\u5417\uff1f\u4efb\u4f55\u5e2e\u52a9\u8868\u793a\u8d5e\u8d4f\u3002\u9884\u5148\u611f\u8c22\u60a8<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u786e\u4fdd\u670d\u52a1\u5668\u54cd\u5e94\u90e8\u5206\u5185\u5bb9\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u5bf9\u5faa\u73af\u7684\u4e00\u4e9b\u4fee\u590d\uff0c\u6211\u5047\u8bbe\u5728\u6bcf\u4e2a\u54cd\u5e94\u4e0a\u8fd0\u884c\uff1a\u5c06\u62a5\u544a\u7684\u5b57\u8282\u6570\u9644\u52a0\u5230\u7f13\u51b2\u533a\u3002\u5c06\u8bfb\u53d6\u7684\u4efb\u4f55\u6570\u636e\u590d\u5236\u5230 c.data \u540e\u4e2d\u65ad\u3002\u51fa\u73b0\u4efb\u4f55\u9519\u8bef\u65f6\u4e2d\u65ad\u3002<\/p>\n<pre>for {\n    n, err := io.ReadFull(res.Body, buffer)\n    c.data = append(c.data, buffer[:n]...)\n    if err != nil {\n        break\n    }\n\n    runtime.EventsEmit(c.ctx, \"transfered\", c.index, n)\n}<\/pre>\n<p>\u65e0\u9700\u4f7f\u7528 content-range \u6765\u62a5\u544a\u8fdb\u5ea6\u3002\u8bf7\u6c42\u6574\u4e2a\u8d44\u6e90\u5e76\u62a5\u544a\u8bfb\u53d6\u7684\u5b57\u8282\uff0c\u5982\u4e0a\u9762\u7684\u5faa\u73af\u6240\u793a\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300agolang\u4e2d\u5982\u4f55\u4ece\u8f83\u5c0f\u7684\u5b57\u8282\u6570\u7ec4\u586b\u5145\u8f83\u5927\u7684\u5b57\u8282\u6570\u7ec4\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; golang\u4e2d\u5982\u4f55\u4ece\u8f83\u5c0f\u7684\u5b57\u8282\u6570\u7ec4\u586b\u5145\u8f83\u5927\u7684\u5b57\u8282\u6570\u7ec4\uff1f golang\u4e2d\u5982\u4f55\u4ece\u8f83\u5c0f\u7684\u5b57\u8282\u6570\u7ec4\u586b\u5145\u8f83\u5927\u7684\u5b57\u8282\u6570\u7ec4\uff1f \u6765\u6e90\uff1astackoverflow 2024-04-19 18:45:36 0\u6d4f\u89c8 \u6536\u85cf \u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300agolang\u4e2d\u5982\u4f55\u4ece\u8f83\u5c0f\u7684\u5b57\u8282\u6570\u7ec4\u586b\u5145\u8f83\u5927\u7684\u5b57\u8282\u6570\u7ec4\uff1f\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002 \u95ee\u9898\u5185\u5bb9 \u56e0\u6b64\uff0c\u6211\u6b63\u5728\u4ece url \u4e0b\u8f7d\u4e00\u4e2a\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u62c6\u5206\u4e3a\u5e26\u6709 content-range \u6807\u5934\u7684\u5757\uff0c\u5e76\u4e14\u5e0c\u671b\u5c06\u4e0b\u8f7d\u6587\u4ef6\u6d41\u5f0f\u4f20\u8f93\u5230\u8fdb\u5ea6\u680f\u3002\u76ee\u524d\uff0c\u6211\u5728\u5c06\u4e0b\u8f7d\u7684\u5b57\u8282\u5199\u5165\u5757\u65f6\u9047\u5230\u4e86\u56f0\u96be\u3002\u6211\u7684\u505a\u6cd5\u5982\u4e0b\uff1a type Chunk struct { response _http.Response wg *sync.WaitGroup index int start int64 end int64 size int64 data []byte ctx context.Context } func New(ctx context.Context, res _http.Response, index int, wg *sync.WaitGroup) *Chunk { totalpart := int64(res.Totalpart) partsize [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-41296","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/41296","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=41296"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/41296\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=41296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=41296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=41296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}