-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_2.go
56 lines (48 loc) · 1.16 KB
/
main_2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"fmt"
"io"
"net/http"
"os"
)
func main() {
// Use a goroutine to download the image concurrently
go downloadImage("https://www.example.com/image.jpg")
}
func downloadImage(url string) {
// Create an HTTP client
client := &http.Client{}
// Create a request to the specified URL
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println("Error creating request:", err)
return
}
// Send the request and get the response
res, err := client.Do(req)
if err != nil {
fmt.Println("Error downloading image:", err)
return
}
defer res.Body.Close()
// Create a local file to store the image
file, err := os.Create("image.jpg")
if err != nil {
fmt.Println("Error creating local file:", err)
return
}
defer file.Close()
// Write the image data to the local file
_, err = io.Copy(file, res.Body)
if err != nil {
fmt.Println("Error writing to local file:", err)
return
}
// Get the size of the downloaded image
fileInfo, err := file.Stat()
if err != nil {
fmt.Println("Error getting file info:", err)
return
}
fmt.Println("Image size:", fileInfo.Size())
}