-1

I have a config file. I want to get specific value from that file. Here is my code :

package main

import (
    "fmt"
    "os"
    "encoding/json"
)

type Configuration struct {
    consumer_key   string
    consumer_secret   string
    access_token   string
    access_token_secret   string
    db_name   string
    db_user   string
    db_password   string
    secret_key   string
    fb_page   string
    fb_page_token   string
    domain   string
}

func main() {
    file, _ := os.Open("./config.json")
    decoder := json.NewDecoder(file)
    configuration := Configuration{}
    error_ := decoder.Decode(&configuration)
     fmt.Println(configuration.domain)
}

config.json

{
  "consumer_key": "",
  "consumer_secret": "",
  "access_token": "",
  "access_token_secret": "",
  "db_name": "",
  "db_user": "",
  "db_password": "",
  "secret_key": "",
  "fb_page": "",
  "fb_page_token": "",
  "domain": "localhost:8000"
}

But the problem is it always print empty line, not the value localhost:8000 I am expecting.

Eugene Lisitsky
  • 12,113
  • 5
  • 38
  • 59
pyprism
  • 2,928
  • 10
  • 46
  • 85

1 Answers1

1

You need to export Configuration.domain field so that json package can see it.

Rename the domain field to Domain:

type Configuration struct {
  Domain string
}

You can also specify json name explicitly if it differs from field name:

type Configuration struct {
  Domain string `json:"domain_name"`
}
Pavel K.
  • 6,697
  • 8
  • 49
  • 80