Advertisement
cydside

Reading a JSON into a compliant struct

Apr 18th, 2023 (edited)
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.41 KB | None | 0 0
  1. //______________________________________________________________________________
  2.  
  3. type Config struct {
  4.     BBoltFile  string `json:"bbolt_file"`
  5.     SqliteFile string `json:"sqlite_file"`
  6.     BucketPath string `json:"bucket_path"`
  7.     TableName  string `json:"table_name"`
  8.     Fields     []Corr `json:"fields"`
  9. }
  10.  
  11. //______________________________________________________________________________
  12.  
  13. type Corr struct {
  14.     ReadName  string `json:"read"`
  15.     WriteName string `json:"write"`
  16. }
  17.  
  18. //______________________________________________________________________________
  19.  
  20. // Reads config.json and decode into AppConfig
  21. func loadAppConfig(configFile string, appcfg *Config) {
  22.     file, err := os.Open(configFile)
  23.     defer file.Close()
  24.     if err != nil {
  25.         log.Fatalf("[loadConfig] File: %s Error: %s\n", configFile, err)
  26.     }
  27.     decoder := json.NewDecoder(file)
  28.     err = decoder.Decode(&appcfg)
  29.     if err != nil {
  30.         fmt.Println("configuration's reading report: ", err.Error())
  31.         os.Exit(-1)
  32.     }
  33.  
  34.     fmt.Printf("%+v\n", appcfg)
  35. }
  36.  
  37. /*
  38.  
  39. {
  40.     "bbolt_file": "",
  41.     "sqlite_file": "",
  42.     "bucket_path": "",
  43.     "table_name": "",
  44.     "fields": [
  45.         {
  46.             "read": "",
  47.             "write": ""
  48.         },
  49.         {
  50.             "read": "",
  51.             "write": ""
  52.         },
  53.         {
  54.             "read": "",
  55.             "write": ""
  56.         },
  57.         {
  58.             "read": "",
  59.             "write": ""
  60.         },
  61.         {
  62.             "read": "",
  63.             "write": ""
  64.         },
  65.         {
  66.             "read": "",
  67.             "write": ""
  68.         },
  69.         {
  70.             "read": "",
  71.             "write": ""
  72.         }
  73.     ]
  74. }
  75.  
  76. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement