golang實(shí)現(xiàn)多協(xié)程下載文件(支持?jǐn)帱c(diǎn)續(xù)傳)
引言
寫這篇文章主要是周末休息太無聊,看了看別人代碼,發(fā)現(xiàn)基本上要么是多協(xié)程下載文件要么就只有單協(xié)程的斷點(diǎn)續(xù)傳,所以就試了試有進(jìn)度條的多協(xié)程下載文件(支持?jǐn)帱c(diǎn)續(xù)傳)
package main
import (
"fmt"
"io"
"os"
"regexp"
"strconv"
"sync"
"github.com/qianlnk/pgbar"
)
/**
* 需求:
1. 多協(xié)程下載文件
2.斷點(diǎn)續(xù)連
**/
func main() {
//獲取要下載文件
DownloadFileName := "./123.zip"
//copy的文件
copyFileName := "./test.zip"
storgeFileName := "./current.txt"
//打開文件
sfile, err := os.Open(DownloadFileName)
if err != nil {
panic(err)
}
defer sfile.Close()
//獲取文件大小
info, _ := sfile.Stat()
downloadSize := info.Size()
var scount int64 = 1
if downloadSize%5 == 0 {
scount *= 5
} else {
scount *= 10
}
//分給每個協(xié)程的大小
si := downloadSize / scount
fmt.Printf("文件總大?。?v, 分片數(shù):%v,每個分片大?。?v\n", downloadSize, scount, si)
//open copy file
copyFile, err := os.OpenFile(copyFileName, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
panic(err)
}
storgeFile, err := os.OpenFile(storgeFileName, os.O_CREATE|os.O_RDWR, os.ModePerm)
if err != nil {
panic(err)
}
defer copyFile.Close()
var currentIndex int64 = 0
wg := sync.WaitGroup{}
fmt.Println("協(xié)程進(jìn)度條")
pgb := pgbar.New("")
for ; currentIndex < scount; currentIndex++ {
wg.Add(1)
go func(current int64) {
p := pgb.NewBar(fmt.Sprint((current+1))+"st", int(si))
// p.SetSpeedSection(900, 100)
b := make([]byte, 1024)
bs := make([]byte, 16)
currentIndex, _ := storgeFile.ReadAt(bs, current*16)
//取出所有整數(shù)
reg := regexp.MustCompile(`\d+`)
countStr := reg.FindString(string(bs[:currentIndex]))
total, _ := strconv.ParseInt(countStr, 10, 0)
progressBar := 1
for {
if total >= si {
wg.Done()
break
}
//從指定位置開始讀
n, err := sfile.ReadAt(b, current*si+total)
if err == io.EOF {
wg.Done()
break
}
//從指定位置開始寫
copyFile.WriteAt(b, current*si+total)
storgeFile.WriteAt([]byte(strconv.FormatInt(total, 10)+" "), current*16)
total += int64(n)
if total >= si/10*int64(progressBar) {
progressBar += 1
p.Add(int(si / 10))
}
}
}(currentIndex)
}
wg.Wait()
storgeFile.Close()
os.Remove(storgeFileName)
fmt.Println("下載完成")
}
到此這篇關(guān)于golang實(shí)現(xiàn)多協(xié)程下載文件(支持?jǐn)帱c(diǎn)續(xù)傳)的文章就介紹到這了,更多相關(guān)golang 多協(xié)程下載文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
golang利用函數(shù)閉包實(shí)現(xiàn)簡單的中間件
中間件設(shè)計(jì)模式是一種常見的軟件設(shè)計(jì)模式,它在許多編程語言和框架中被廣泛應(yīng)用,這篇文章主要為大家介紹一下golang利用函數(shù)閉包實(shí)現(xiàn)一個簡單的中間件,感興趣的可以了解下2023-10-10
golang gopm get -g -v 無法獲取第三方庫的解決方案
這篇文章主要介紹了golang gopm get -g -v 無法獲取第三方庫的解決方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-05-05
VS Code配置Go語言開發(fā)環(huán)境的詳細(xì)教程
這篇文章主要介紹了VS Code配置Go語言開發(fā)環(huán)境的詳細(xì)教程,本文通過實(shí)例代碼圖文相結(jié)合的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05
Go中string與[]byte高效互轉(zhuǎn)的方法實(shí)例
string與[]byte經(jīng)常需要互相轉(zhuǎn)化,普通轉(zhuǎn)化會發(fā)生底層數(shù)據(jù)的復(fù)制,下面這篇文章主要給大家介紹了關(guān)于Go中string與[]byte高效互轉(zhuǎn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09
Go type關(guān)鍵字(類型定義與類型別名的使用差異)用法實(shí)例探究
這篇文章主要為大家介紹了Go type關(guān)鍵字(類型定義與類型別名的使用差異)用法實(shí)例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01

