如何利用 Go 中的 Goroutines 從 Dropbox 同時下載多個檔案?
Go 中使用 Goroutines 並行檔案下載
在 Go 中,可以使用 Goroutines 並行下載和儲存檔案。 Goroutines 允許輕量級執行緒執行,促進並行處理並提高效能。
這是您提供的程式碼,經過修改以利用 Goroutines:
<code class="go">package main import ( "encoding/json" "fmt" "io" "io/ioutil" "net/http" "net/url" "os" "path/filepath" "sync" ) // Credentials const app_key string = "<app_key>" const app_secret string = "<app_secret>" // Authorization code var code string // Token response type TokenResponse struct { AccessToken string `json:"access_token"` } // File structure type File struct { Path string } // File list response type FileListResponse struct { FileList []File `json:"contents"` } // Download a file using goroutines func download_file(file File, token TokenResponse, wg *sync.WaitGroup) { download_file := fmt.Sprintf("https://api-content.dropbox.com/1/files/dropbox/%s?access_token=%s", file.Path, token.AccessToken) resp, _ := http.Get(download_file) defer resp.Body.Close() filename := filepath.Base(file.Path) out, err := os.Create(filename) if err != nil { panic(err) } defer out.Close() io.Copy(out, resp.Body) wg.Done() } func main() { // Authorization and token retrieval authorize_url := fmt.Sprintf("https://www.dropbox.com/1/oauth2/authorize?response_type=code&client_id=%s", app_key) fmt.Printf("1. Go to: %s\n", authorize_url) fmt.Println("2. Click 'Allow' (you might have to log in first)") fmt.Println("3. Copy the authorization code.") fmt.Printf("Enter the authorization code here: ") fmt.Scanf("%s", &code) // Get file list file_list_url := fmt.Sprintf("https://api.dropbox.com/1/metadata/dropbox/Camera Uploads?access_token=%s", tr.AccessToken) resp2, _ := http.Get(file_list_url) defer resp2.Body.Close() contents2, _ := ioutil.ReadAll(resp2.Body) var flr FileListResponse json.Unmarshal(contents2, &flr) // WaitGroup to wait for all goroutines to finish var wg sync.WaitGroup // Download files concurrently for i, file := range flr.FileList { wg.Add(1) go download_file(file, tr, &wg) if i >= 2 { break } } wg.Wait() }</code>
在此修改後的程式碼中,我們新增了一個sync.sync。 WaitGroup 呼叫 wg 來追蹤正在執行的 goroutine 數量。我們在啟動每個 goroutine 之前遞增 wg,並在每個 goroutine 使用 wg.Done() 完成時遞減 wg。主協程透過呼叫 wg.Wait() 等待所有協程完成。這可以確保在下載所有文件之前程式不會退出。
以上是如何利用 Go 中的 Goroutines 從 Dropbox 同時下載多個檔案?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

OpenSSL,作為廣泛應用於安全通信的開源庫,提供了加密算法、密鑰和證書管理等功能。然而,其歷史版本中存在一些已知安全漏洞,其中一些危害極大。本文將重點介紹Debian系統中OpenSSL的常見漏洞及應對措施。 DebianOpenSSL已知漏洞:OpenSSL曾出現過多個嚴重漏洞,例如:心臟出血漏洞(CVE-2014-0160):該漏洞影響OpenSSL1.0.1至1.0.1f以及1.0.2至1.0.2beta版本。攻擊者可利用此漏洞未經授權讀取服務器上的敏感信息,包括加密密鑰等。

在BeegoORM框架下,如何指定模型關聯的數據庫?許多Beego項目需要同時操作多個數據庫。當使用Beego...

後端學習路徑:從前端轉型到後端的探索之旅作為一名從前端開發轉型的後端初學者,你已經有了nodejs的基礎,...

GoLand中自定義結構體標籤不顯示怎麼辦?在使用GoLand進行Go語言開發時,很多開發者會遇到自定義結構體標籤在�...

Go語言中使用RedisStream實現消息隊列時類型轉換問題在使用Go語言與Redis...

Go語言中用於浮點數運算的庫介紹在Go語言(也稱為Golang)中,進行浮點數的加減乘除運算時,如何確保精度是�...

Go爬蟲Colly中的Queue線程問題探討在使用Go語言的Colly爬蟲庫時,開發者常常會遇到關於線程和請求隊列的問題。 �...

本文介紹如何在Debian系統上配置MongoDB實現自動擴容,主要步驟包括MongoDB副本集的設置和磁盤空間監控。一、MongoDB安裝首先,確保已在Debian系統上安裝MongoDB。使用以下命令安裝:sudoaptupdatesudoaptinstall-ymongodb-org二、配置MongoDB副本集MongoDB副本集確保高可用性和數據冗餘,是實現自動擴容的基礎。啟動MongoDB服務:sudosystemctlstartmongodsudosys
