从项目学习 Golang (1) - 读取 ini 配置文件, 操作 redis, 数据类型转换, 时间日志

抓取 Binance 的盘口价格 (1)

读取 ini 配置文件

从网上抄的一段代码, 对 ini 文件读取方法的再封装:

安装依赖包 go get gopkg.in/ini.v1

点击查看 utils 模块封装
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package utils

import (
"gopkg.in/ini.v1"
)

type IniParser struct {
conf_reader *ini.File // config reader
}

type IniParserError struct {
error_info string
}

func (e *IniParserError) Error() string { return e.error_info }

func (this *IniParser) Load(config_file_name string) error {
conf, err := ini.Load(config_file_name)
if err != nil {
this.conf_reader = nil
return err
}
this.conf_reader = conf
return nil
}

func (this *IniParser) GetString(section string, key string) string {
if this.conf_reader == nil {
return ""
}

s := this.conf_reader.Section(section)
if s == nil {
return ""
}

return s.Key(key).String()
}

func (this *IniParser) GetInt32(section string, key string) int32 {
if this.conf_reader == nil {
return 0
}

s := this.conf_reader.Section(section)
if s == nil {
return 0
}

value_int, _ := s.Key(key).Int()

return int32(value_int)
}

func (this *IniParser) GetUint32(section string, key string) uint32 {
if this.conf_reader == nil {
return 0
}

s := this.conf_reader.Section(section)
if s == nil {
return 0
}

value_int, _ := s.Key(key).Uint()

return uint32(value_int)
}

func (this *IniParser) GetInt64(section string, key string) int64 {
if this.conf_reader == nil {
return 0
}

s := this.conf_reader.Section(section)
if s == nil {
return 0
}

value_int, _ := s.Key(key).Int64()
return value_int
}

func (this *IniParser) GetUint64(section string, key string) uint64 {
if this.conf_reader == nil {
return 0
}

s := this.conf_reader.Section(section)
if s == nil {
return 0
}

value_int, _ := s.Key(key).Uint64()
return value_int
}

func (this *IniParser) GetFloat32(section string, key string) float32 {
if this.conf_reader == nil {
return 0
}

s := this.conf_reader.Section(section)
if s == nil {
return 0
}

value_float, _ := s.Key(key).Float64()
return float32(value_float)
}

func (this *IniParser) GetFloat64(section string, key string) float64 {
if this.conf_reader == nil {
return 0
}

s := this.conf_reader.Section(section)
if s == nil {
return 0
}

value_float, _ := s.Key(key).Float64()
return value_float
}
点击查看 ini 文件的读取

// utils/conf.ini

1
2
3
4
5
6
7
[main]

[redis]
host: 127.0.0.1
port: 6379
db: 0
auth:

// main.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import "project/utils"

func readConf() (addr string, err error) {
iniParser := utils.IniParser{}
confFilename := "conf.ini"

if err := iniParser.Load(confFilename); err != nil {
fmt.Printf("try load config file[%s] error[%s]\n", confFilename, err.Error())
return nil, err
}

fmt.Println(iniParser.GetString("redis", "host") + ":" + iniParser.GetString("redis", "port"))
return iniParser.GetString("redis", "host") + ":" + iniParser.GetString("redis", "port"), nil
}

redis 链接与操作

安装包依赖 go get github.com/go-redis/redis

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
func connectRedis(conf *redis.Options) (client *redis.Client, err error) {
conf, err := readRedisConf(conf)
if err != nil {
fmt.Println("read redis conf failed", err)
return nil, err
}

client = redis.NewClient(conf)
if err != nil {
fmt.Println("redis connect failed", err)
return nil, err
}

return client, nil
}

conf := &redis.Options{
Addr: "127.0.0.1:6379",
Password: "",
DB: 0,
}

client, err := connectRedis(conf)
if err != nil {
fmt.Println("redis connect failed", err)
}

if err = client.Set("aaa", "bbb", 0).Err(); err != nil {
fmt.Println("redis set aaa", err)
}

数据类型的转换

从 ini 中读取配置文件, 可以拿到 string, int32, int64, uint32, uint64, float32, float64 几种类型.

1
2
3
4
5
6
7
8
9
10
11
12
// string => int
int, err := strconv.Atoi(string)
// int => string
string := strconv.Itoa(int)

// string => int64
int64, err := strconv.ParseInt(string, 10, 64)
// int64 => string
string := strconv.FormatInt(int64, 10)

// int64 => int
int := int(int64)

输出时间格式

import "time"

  • 可读时间: timeStr := time.Now().Format("2006-01-02 15:04:05"), 其中 “2006-01-02 15:04:05” 为固定时间, golang 诞生时间.
  • 秒时间戳: s := time.Now().Unix()
  • 纳秒时间戳: ns := time.Now().UnixNano()
1
2
3
4
5
// Y-m-d H:i:s => Time
t := time.Parse("2006-01-02 15:04:05", str)

// timestamp => Y-m-d H:i:s
s := time.Unix(t, 0).Format("2006-01-02 15:04:05")
Donate - Support to make this site better.
捐助 - 支持我让我做得更好.