-
Notifications
You must be signed in to change notification settings - Fork 168
/
utility.go
56 lines (53 loc) · 954 Bytes
/
utility.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
/*
* @Author: SpenserCai
* @Date: 2023-02-21 10:44:10
* @version:
* @LastEditors: SpenserCai
* @LastEditTime: 2023-02-23 16:12:29
* @Description: file content
*/
package main
import (
"io"
"io/ioutil"
"net/http"
"os"
)
func CopyFile(src, dst string) error {
// 判断源文件是否存在
_, err := os.Stat(src)
if err != nil {
return err
}
// 读取源文件
srcFile, err := os.Open(src)
if err != nil {
return err
}
defer srcFile.Close()
// 创建目标文件
dstFile, err := os.Create(dst)
if err != nil {
return err
}
defer dstFile.Close()
// 拷贝文件
_, err = io.Copy(dstFile, srcFile)
if err != nil {
return err
}
return nil
}
// 获取公网ip
func GetPublicIp() (string, error) {
resp, err := http.Get("http://myexternalip.com/raw")
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}