如何在文件下载时打印字节? -golang [英] How to print the bytes while the file is being downloaded ? -golang

查看:138
本文介绍了如何在文件下载时打印字节? -golang的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以计算并打印下载文件时下载的字节数。

  out,err:= os.Create(file.txt)
推迟out.Close()
if err!= nil {
fmt.Println(fmt.Sprint(err))

panic(err)
}
resp,err:= http.Get(http://example.com/zip)
推迟resp.Body.Close()
if err!= nil {
fmt.Println(fmt.Sprint( err))
panic(err)
}
$ b $,er:= io.Copy(out,resp.Body)
if!!= nil {
fmt.Println(fmt.Sprint(err))
}
fmt.Println(n,bytes)

由于某些原因,SO不会让我提交这个问题,因为看起来您的文章主要是代码;请添加更多的细节,所以我只是写如果我正确理解你的话,你希望显示读取的字节数,而数据是这样的。 转移。大概保持某种进度条或某种东西。
在这种情况下,您可以使用Go的组合数据结构将读写器封装在自定义 io.Reader io.Writer中 $ b

它简单地转发相应的读取调用写入基础流,同时使用它们返回的(int,error)值进行一些额外的工作。以下是您可以在去操场上运行的示例。

 包主要

导入(
字节
fmt
io
os
字符串


// PassThru包装一个现有的io.Reader。
//
//它只是转发Read()调用,同时显示
//单个调用的结果。
类型PassThru struct {
io.Reader
total int64 //传输字节总数
}

//读取'overrides'底层io .Reader的读取方法。
//这将是由io.Copy()调用的。我们只需
//用它来跟踪字节数,然后转发呼叫。
func(pt * PassThru)Read(p [] byte)(int,error){
n,err:= pt.Reader.Read(p)
pt.total + = int64 n)

if err == nil {
fmt.Println(Read,n,bytes for total,pt.total)
}

return n,err
}

func main(){
var src io.Reader //源文件/ url / etc
var dst bytes.Buffer //目标文件/缓冲区/等

//创建一些随机输入数据。
src = bytes.NewBufferString(strings.Repeat(一些随机输入数据,1000))

//用我们自定义的io.Reader包装它。
src =& PassThru {Reader:src}
$ b $ count,err:= io.Copy(& dst,src)
if err!= nil {
fmt.Println(err)
os.Exit(1)
}

fmt.Println(转移,计数,字节)
}

它产生的输出是这样的:

 读512个字节,总数为512 
读1024个字节,总数为1536
读2048字节,总数为3584
读4096字节总共7680
阅读8192个字节,共计15872
阅读6128字节,总计22000
转移22000字节


I'm wondering if it's possible to count and print the number of bytes downloaded while the file is being downloaded.

out, err := os.Create("file.txt")
    defer out.Close()
    if err != nil {
        fmt.Println(fmt.Sprint(err) )

        panic(err)
    }
    resp, err := http.Get("http://example.com/zip")
    defer resp.Body.Close()
    if err != nil {
        fmt.Println(fmt.Sprint(err) )
        panic(err)
    }

    n, er := io.Copy(out, resp.Body)
    if er != nil {
        fmt.Println(fmt.Sprint(err) )
    }
    fmt.Println(n, "bytes ")

For some reasons SO doesn't let me submit the question because is "it looks like your post is mostly code; please add some more detail" so I'm just writing this

解决方案

If I understand you correctly, you wish to display the number of bytes read, while the data is transferring. Presumably to maintain some kind of a progress bar or something. In which case, you can use Go's compositional data structures to wrap the reader or writer in a custom io.Reader or io.Writer implementation.

It simply forwards the respective Read or Write call to the underlying stream, while doing some additional work with the (int, error) values returned by them. Here is an example you can run on the Go playground.

package main

import (
    "bytes"
    "fmt"
    "io"
    "os"
    "strings"
)

// PassThru wraps an existing io.Reader.
//
// It simply forwards the Read() call, while displaying
// the results from individual calls to it.
type PassThru struct {
    io.Reader
    total int64 // Total # of bytes transferred
}

// Read 'overrides' the underlying io.Reader's Read method.
// This is the one that will be called by io.Copy(). We simply
// use it to keep track of byte counts and then forward the call.
func (pt *PassThru) Read(p []byte) (int, error) {
    n, err := pt.Reader.Read(p)
    pt.total += int64(n)

    if err == nil {
        fmt.Println("Read", n, "bytes for a total of", pt.total)
    }

    return n, err
}

func main() {
    var src io.Reader    // Source file/url/etc
    var dst bytes.Buffer // Destination file/buffer/etc

    // Create some random input data.
    src = bytes.NewBufferString(strings.Repeat("Some random input data", 1000))

    // Wrap it with our custom io.Reader.
    src = &PassThru{Reader: src}

    count, err := io.Copy(&dst, src)
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }

    fmt.Println("Transferred", count, "bytes")
}

The output it generates is this:

Read 512 bytes for a total of 512
Read 1024 bytes for a total of 1536
Read 2048 bytes for a total of 3584
Read 4096 bytes for a total of 7680
Read 8192 bytes for a total of 15872
Read 6128 bytes for a total of 22000
Transferred 22000 bytes

这篇关于如何在文件下载时打印字节? -golang的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆