title: How Do I Parse a JSON file into a struct with Go
body: <p>I'm trying to configure my Go program by creating a JSON file and parsing it into a struct:</p>
  
  <pre><code>var settings struct {
      serverMode bool
      sourceDir  string
      targetDir  string
  }
  
  func main() {
  
      // then config file settings
  
      configFile, err := os.Open("config.json")
      if err != nil {
          printError("opening config file", err.Error())
      }
  
      jsonParser := json.NewDecoder(configFile)
      if err = jsonParser.Decode(&amp;settings); err != nil {
          printError("parsing config file", err.Error())
      }
  
      fmt.Printf("%v %s %s", settings.serverMode, settings.sourceDir, settings.targetDir)
      return
  }
  </code></pre>
  
  <p>The config.json file:</p>
  
  <pre><code>{
      "serverMode": true,
      "sourceDir": ".",
      "targetDir": "."
  }
  </code></pre>
  
  <p>The Program compiles and runs without any errors, but the print statement outputs:</p>
  
  <pre><code>false  
  </code></pre>
  
  <p>(false and two empty strings)</p>
  
  <p>I've also tried with <code>json.Unmarshal(..)</code> but had the same result.</p>
  
  <p>How do I parse the JSON in a way that fills the struct values?</p>