mirror of
https://gitee.com/shikong-sk/golang-study
synced 2025-02-22 23:12:15 +08:00
docs: ini 文件解析
reflect 反射 解析 ini
This commit is contained in:
parent
ce1b89f984
commit
656dfbac05
@ -4,6 +4,7 @@ host=127.0.0.1
|
||||
port=3306
|
||||
user=root
|
||||
password=12341234
|
||||
master = true
|
||||
# redis 配置
|
||||
[redis]
|
||||
host= 127.0.0.1
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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"))
|
||||
|
||||
// 字符串 转 时间对象
|
||||
|
Loading…
Reference in New Issue
Block a user