-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage.go
More file actions
52 lines (44 loc) · 811 Bytes
/
Image.go
File metadata and controls
52 lines (44 loc) · 811 Bytes
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
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
"strings"
)
var folderPath = "img/"
func GetImg(url string) (err error) {
path := strings.Split(url, "/")
var name string
if len(path) > 1 {
name = folderPath + path[len(path)-1]
}
fmt.Println("path :", path)
fmt.Println("name :", name)
os.MkdirAll("img", os.ModePerm)
// 創建寫入檔案串流的io.Writer
out, err := os.Create(name)
if err != nil {
return err
}
defer out.Close()
fmt.Println("out: ", out)
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
fmt.Println("resp: ", resp)
// base64 data
data, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
n, err := io.Copy(out, bytes.NewReader(data))
if err != nil {
return err
}
fmt.Println("n: ", n)
return
}