How Do I Parse a JSON file into a struct with Go

后端 未结 1 1536
挽巷
挽巷 2020-12-14 06:49

I\'m trying to configure my Go program by creating a JSON file and parsing it into a struct:

var settings struct {
    serverMode bool
    sourceDir  string
         


        
相关标签:
1条回答
  • 2020-12-14 06:53

    You're not exporting your struct elements. They all begin with a lower case letter.

    var settings struct {
        ServerMode bool `json:"serverMode"`
        SourceDir  string `json:"sourceDir"`
        TargetDir  string `json:"targetDir"`
    }
    

    Make the first letter of your stuct elements upper case to export them. The JSON encoder/decoder wont use struct elements which are not exported.

    0 讨论(0)
提交回复
热议问题