使用go协程和channel管道实现下载

    1. package main
    2. import (
    3. "fmt"
    4. "io/ioutil"
    5. "net/http"
    6. "os"
    7. // "strconv"
    8. "strings"
    9. )
    10. func download(url string, name string, ret chan<- struct{}) {
    11. // fmt.Println(name)
    12. if name == "" {
    13. names := strings.Split(url, "/")
    14. name = names[len(names)-1]
    15. }
    16. // fmt.Println(name)
    17. response, err := http.Get(url)
    18. defer response.Body.Close()
    19. if err != nil {
    20. fmt.Println(err)
    21. }
    22. body, err := ioutil.ReadAll(response.Body)
    23. if err != nil {
    24. fmt.Println(err)
    25. }
    26. file, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE, 0666)
    27. if err != nil {
    28. fmt.Println(err)
    29. }
    30. defer file.Close()
    31. _, er := file.Write(body)
    32. if er != nil {
    33. fmt.Println(er)
    34. }
    35. // fmt.Println(string(body))
    36. // close(ret)
    37. ret <- struct{}{}
    38. }
    39. func main() {
    40. urls := []string{"https://gloimg.zafcdn.com/zaful/pdm-product-pic/Clothing/2017/10/23/source-img/20171023104754_59693.png", "https://gloimg.zafcdn.com/zaful/pdm-product-pic/Clothing/2018/05/26/source-img/20180526163835_98732.png"}
    41. ret := make(chan struct{}, len(urls))
    42. // fmt.Println(len(urls))
    43. i := 0
    44. for _, url := range urls {
    45. go download(url, "", ret)
    46. i += 2
    47. }
    48. // select {}
    49. for i := 0; i < len(urls); i++ {
    50. // fmt.Println(<-ret)
    51. <-ret
    52. }
    53. }

    result:
    并发下载网络资源并保存 - 图1