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"`  }  ​  // 定义配置文件结构体  type JsonStruct struct {  }  ​  // 创建配置文件的构造函数  func NewJsonStruct() *JsonStruct {   return &JsonStruct{}  }  ​  // 加载配置文件  func (jt *JsonStruct) Load(filename string, v interface{}) {   // 读取配置文件   data, err := ioutil.ReadFile(filename)   if err != nil {   return   }   // 解析配置文件   err = json.Unmarshal(data, v)   if err != nil {   return   }  ​  }  ​  func main() {   JsonParse := NewJsonStruct()   v := Config{}   // 获取配置文件路径   osGwd, _ := os.Getwd()   confPath := osGwd + "/conf_json.json"   // 加载配置文件   JsonParse.Load(confPath, &v)   fmt.Printf("配置文件的类型为 %s \n", v.Type)   fmt.Printf("PG 数据库的配置为 %s \n", v.Postgres)  }  ​