Go 常用代码块

总结备忘一下常用的的 Go 代码片段

遍历目录下的文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
func getFilelist(r string) {
err := filepath.Walk(r, func(p string, f os.FileInfo, err error) error {
if f == nil {
return nil
}
if p == r || f.IsDir() {
return nil
}
fmt.Println(p)
return nil
})
if err != nil {
fmt.Printf("filepath.Walk() returned %v\n", err)
}
}

定时器

1
2
3
4
5
6
7
8
9
10
11
duration := 2 * time.Second
timer := time.NewTimer(duration)
go func() {
for {
select {
case <-timer.C:
fmt.Println("here")
timer.Reset(duration)
}
}
}()

定时执行任务

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
package main
import (
"fmt"
"time"
)
const INTERVAL_PERIOD time.Duration = 24 * time.Hour
const HOUR_TO_TICK int = 23
const MINUTE_TO_TICK int = 00
const SECOND_TO_TICK int = 03
func main() {
ticker := updateTicker()
for {
<-ticker.C
fmt.Println(time.Now(), "- just ticked")
ticker = updateTicker()
}
}
func updateTicker() *time.Ticker {
n := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(),
HOUR_TO_TICK, MINUTE_TO_TICK, SECOND_TO_TICK, 0, time.Local)
if !n.After(time.Now()) {
n = n.Add(INTERVAL_PERIOD)
}
fmt.Println(n, "- next tick")
diff := n.Sub(time.Now())
return time.NewTicker(diff)
}

读取文件

1
2
3
4
5
6
7
8
9
10
f, err := os.Open("/test.txt")
if err != nil {
panic(err)
}
defer f.Close()
fd, err := ioutil.ReadAll(f)
if err != nil {
panic(err)
}
fmt.Println(string(fd))

map 转 json

1
2
3
4
5
6
7
8
m := make(map[string]interface{})
m["show"] = "1"
m["content"] = "test"
j, err := json.Marshal(m)
if err != nil {
panic(err)
}
fmt.Println(string(j))

json 转 map

1
2
3
4
5
6
7
8
9
10
11
var result = []byte(`
{
"show": 1,
"content": "test"
}`)
var r map[string]interface{}
if err := json.Unmarshal(result, &r); err != nil {
panic(err)
}
fmt.Println(r)

写文件

1
2
3
4
5
6
7
8
9
10
func writeFile(path string, b []byte) {
file, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0777)
defer file.Close()
if err != nil {
panic(err)
}
file.Write(b)
}

简单的获取 IP

1
2
3
4
5
6
7
func GetPulicIP() string {
conn, _ := net.Dial("udp", "8.8.8.8:80")
defer conn.Close()
localAddr := conn.LocalAddr().String()
idx := strings.LastIndex(localAddr, ":")
return localAddr[0:idx]
}
欣赏此文?求鼓励,求支持!
显示 Disqus 评论
0%