gownload

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 30, 2021 License: MIT Imports: 5 Imported by: 0

README

Gownload

Go version of pewn. Allows you to Download file(s) easily.

Installation

  • Initialize your project with go mod init <name>.
  • Get the package with go get github.com/5elenay/gownload.

API Reference

Click here for API reference.

Example

Download single file example

package main

import (
    "fmt"

    "github.com/5elenay/gownload"
)

func main() {
    // Create an option
    option := gownload.Options{
        Name:   "photo.png",
        Folder: []string{"path", "to", "photos"},
    }

    // Download file.
    res := gownload.Download("https://picsum.photos/500/300", option)

    // An helper function for gownload.Result which allows you to handle result and error easily.
    res.Handle(res.Error, func(path string) {
        // Use file path
        fmt.Println(path)
    })
}

Download multiple-file example

package main

import (
    "fmt"

    "github.com/5elenay/gownload"
)

func main() {
    // Create an option.
    option := gownload.Options{
        Name:   "photo.png",
        Folder: []string{"path", "to", "photos"},
    }

    // Make a string slice and add the url 10 times.
    var urls []string

    for i := 0; i < 10; i++ {
        urls = append(urls, "https://picsum.photos/500/300")
    }

    // Download all of the files.
    results := gownload.DownloadMultiple(urls, option)

    // Loop through files
    for _, res := range results {
        // An helper function for gownload.Result which allows you to handle result and error easily.
        res.Handle(res.Error, func(path string) {
            // Use file path
            fmt.Println(path)
        })
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	Name   string
	Folder []string
}

type Result

type Result struct {
	Error error
	Path  string
}

func Download

func Download(url string, option Options) Result

Download a file with Options.

func DownloadMultiple

func DownloadMultiple(urls []string, option Options) []Result

Download Multiple file at same time. File names will be like photo_1.png, photo_2.png ...

func (Result) Handle

func (result Result) Handle(msg error, fn func(path string))

Unpack & handle result easily. Will panic with given message if Result has an error, Otherwise it will run the given function.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL