-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.go
79 lines (69 loc) · 1.23 KB
/
functions.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
package com
import (
"fmt"
"io/ioutil"
"math"
"net/http"
"strings"
)
const (
_VERSION = "0.1.0307"
)
func Version() string {
return _VERSION
}
// 获取用户头像
func GetGravatar(email string) string {
return "http://www.gravatar.com/avatar/" + Md5(strings.ToUpper(email))
}
// 切割关键词为html片段
func TagSplit(keywords string) string {
if "" == keywords {
return ""
}
content := ""
tags := strings.Split(keywords, ",")
for _, value := range tags {
content = content + fmt.Sprintf(`<a class="tags" href="/tag/%s/1">%s</a>,`, value, value)
}
return content
}
// 四舍五入
func Round(val float64, places int) float64 {
var t float64
f := math.Pow10(places)
x := val * f
if math.IsInf(x, 0) || math.IsNaN(x) {
return val
}
if x >= 0.0 {
t = math.Ceil(x)
if (t - x) > 0.50000000001 {
t -= 1.0
}
} else {
t = math.Ceil(-x)
if (t + x) > 0.50000000001 {
t -= 1.0
}
t = -t
}
x = t / f
if !math.IsInf(x, 0) {
return x
}
return t
}
// http GET
func Get(reqUrl string) (string, error) {
response, err := http.Get(reqUrl)
if nil != err {
return "", err
}
body, err := ioutil.ReadAll(response.Body)
if nil != err {
response.Body.Close()
return "", err
}
return string(body), nil
}