Golang 配置文件相关操作
本文以读取数据库配置文件为例
1、JSON 文件
package main /* 解析 json 格式的配置文件 文件内容如下: { "type": "json", "postgres": { "host": "localhost", "port": 5432, "username": "postgres", "password": "postgres", "dbname": "bubble" } } */ import ( "encoding/json" "fmt" "io/ioutil" "os" ) // 定义第一级配置文件的结构体 type Config struct { Type string Postgres DbConf // 数据类型为第二级配置文件的结构体名称 } // 定义第二级配置文件的结构体 注意数据类型 type DbConf struct { Host string `json:"host"` Port uint `json:"port"` Username string `json:"username"` Password string `json:"password"` DbName string `json:"dbname"` } /