我有非常简单的测试:http ://play.golang.org/p/wY4sN9AUky 。从 JSON 解析的配置,第一个字符串值解析成功,但第二个解析为空字符串,但事实并非如此。
type Config struct {
Address string "address"
Debug bool "debug"
DbUrl string "dburl"
GoogleApiKey string "google_api_key"
}
func (cfg *Config) read(json_code string) {
if e := json.Unmarshal([]byte(json_code), cfg); e != nil {
log.Printf("ERROR JSON decode: %v", e)
}
}
func main() {
var config Config
config.read(`{
"address": "10.0.0.2:8080",
"debug": true,
"dburl": "localhost",
"google_api_key": "the-key"
}`)
log.Printf("api key %s", config.GoogleApiKey) // <- empty string. why?
log.Printf("address %v", config.Address)
}