docs: ini 文件解析

reflect 反射 解析 ini
This commit is contained in:
Shikong 2021-10-16 15:22:03 +08:00
parent ce1b89f984
commit 656dfbac05
3 changed files with 15 additions and 1 deletions

View File

@ -4,6 +4,7 @@ host=127.0.0.1
port=3306
user=root
password=12341234
master = true
# redis 配置
[redis]
host= 127.0.0.1

View File

@ -16,6 +16,7 @@ type IniConfig struct {
}
type MysqlConfig struct {
Master bool `ini:"master"`
Host string `ini:"host"`
Port int `ini:"port"`
User string `ini:"user"`
@ -151,6 +152,18 @@ func loadIni(filePath string, v interface{}) (err error) {
sectionVal.Field(i).SetInt(int64(intVal))
case reflect.String:
sectionVal.Field(i).SetString(val)
case reflect.Bool:
boolVal, err := strconv.ParseBool(val)
if err != nil {
return err
}
sectionVal.Field(i).SetBool(boolVal)
case reflect.Float64:
floatVal, err := strconv.ParseFloat(val, 64)
if err != nil {
return err
}
sectionVal.Field(i).SetFloat(floatVal)
}
break
}

View File

@ -69,7 +69,7 @@ func main() {
// 03/15 12/24制 小时
// 04 分
// 05 秒
// 99 毫秒/微/纳秒
// 99 毫秒/微/纳秒
fmt.Println(now.Format("2006-01-02 15:04:05.99"))
// 字符串 转 时间对象