-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
86 lines (79 loc) · 1.52 KB
/
env.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package conf
import (
"log"
"net"
"os"
"runtime"
"strings"
)
// GetEnvironment get the system environment by name.
// GetEnvironment 获取系统环境变量
// parameters envName
// return envValue
func GetEnvironment(envName string) (envValue string) {
envValue = os.Getenv(envName)
if envValue == "" {
log.Print("Getenv null:", envName)
return ""
}
return envValue
}
// GetPid get the proccess id
// GetPid 获取进程ID
// return pid
func GetPid() (pid int) {
getWd := os.Getppid()
return getWd
}
// Getwd get the current working directory
// Getwd 获取当前工作目录
// return getwd
func Getwd() (getwd string, err error) {
getwd, err = os.Getwd()
if err != nil {
log.Println("Getwd|Getwd err:", err)
return "", nil
}
return getwd, nil
}
// GetLocalIPAddr get the local IP address
// GetLocalIPAddr 获取本地IP
// return IP address
func GetLocalIPAddr() string {
addresses, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, addr := range addresses {
ipaddr, _, err := net.ParseCIDR(addr.String())
if err != nil {
continue
}
if ipaddr.IsLoopback() {
continue
}
if ipaddr.To4() != nil {
if runtime.GOOS == "darwin" {
if !strings.HasPrefix(ipaddr.String(), "192") {
continue
}
}
return ipaddr.String()
}
}
return ""
}
func SetEnv(key string, value string) error {
err := os.Setenv(key, value)
if err != nil {
return err
}
return nil
}
func UnSetEnv(key string) error {
err := os.Unsetenv(key)
if err != nil {
return err
}
return nil
}