got package - github.com/g-tool/du - Go Packages

DefaulUserAgent is the default Got user agent to send http requests.

ChunkPool helps in multi *Download files.

DefaultClient is the default http client for got requests.

DefaultFileName is the fallback name for GetFilename.

ErrDownloadAborted - When download is aborted by the OS before it is completed, ErrDownloadAborted will be triggered

GetFilename it returns default file name from a URL.

NewRequest returns a new http.Request and error if any.

Chunk is a partial content range.

type Download struct {
	Client *http.Client

	Concurrency uint

	URL, Dir, Dest string

	Interval, ChunkSize, MinChunkSize, MaxChunkSize uint64

	StopProgress bool
	
}

Download holds downloadable file config and infos.

// Just for testing
destPath := createTemp()
defer clean(destPath)

ctx := context.Background()

dl := got.NewDownload(ctx, testUrl, destPath)

// Init
if err := dl.Init(); err != nil {
	fmt.Println(err)
}

// Start download
if err := dl.Start(); err != nil {
	fmt.Println(err)
}

fmt.Println("Done")
Output:
Done

NewDownload returns new *Download with context.

AvgSpeed returns average download speed.

Context returns download context.

DownloadChunk downloads a file chunk.

GetInfo returns URL info, and error if any.

func (d *Download) Init() (err error)

Init set defaults and split file into chunks and gets Info, you should call Init before Start

func (d Download) IsRangeable() bool

IsRangeable returns file server partial content support state.

Name returns the downloaded file path.

func (d *Download) RunProgress(fn ProgressFunc)

RunProgress runs ProgressFunc based on Interval and updates lastSize.

Size returns downloaded size.

Speed returns download speed.

func (d *Download) Start() (err error)

Start downloads the file chunks, and merges them.

TotalCost returns download duration.

TotalSize returns file total size (0 if unknown).

Write updates progress size.

Got holds got download config.

// Just for testing
destPath := createTemp()
defer clean(destPath)

g := got.New()

err := g.Download(testUrl, destPath)

if err != nil {
	log.Fatal(err)
	return
}

fmt.Println("done")
Output:
done
// Just for testing
destPath := createTemp()
defer clean(destPath)

ctx := context.Background()

g := got.NewWithContext(ctx)

err := g.Download(testUrl, destPath)

if err != nil {
	log.Fatal(err)
	return
}

fmt.Println("done")
Output:
done

New returns new *Got with default context and client.

NewWithContext wants Context and returns *Got with default http client.

Do inits and runs ProgressFunc if set and starts the Download.

Download creates *Download item and runs it.

Info holds downloadable file info.

type ProgressFunc func(d *Download)

ProgressFunc to show progress state, called by RunProgress based on interval.