forked from lycclsltt/system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
var.go
50 lines (44 loc) · 890 Bytes
/
var.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
package system
import (
"io/ioutil"
"os"
"os/exec"
"strconv"
"strings"
)
func Exec(cmd string) (string, error) {
command := exec.Command("sh", "-c", cmd)
bytes, err := command.Output()
return string(bytes), err
}
func Trim(str *string) {
*str = strings.TrimSpace(*str)
*str = strings.Replace(*str, "\n", "", -1)
}
func ExecOutput(cmd string) string {
output, err := Exec(cmd)
if err != nil {
return ""
}
Trim(&output)
return output
}
func FloatToString(f float64) string {
if f == 0 {
//0.00 => 0 减少带宽占用
return "0"
}
return strconv.FormatFloat(f, 'f', 2, 64)
}
func GetFileContent(filePath string) (string, error) {
f, err := os.Open(filePath)
if err != nil {
return "", err
}
defer f.Close()
bytes, err := ioutil.ReadAll(f)
if err != nil {
return "", err
}
return string(bytes), nil
}