Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
读取Go中的json文件值_Json_Go - Fatal编程技术网

读取Go中的json文件值

读取Go中的json文件值,json,go,Json,Go,我有一个配置文件。我想从该文件中获取特定值。这是我的密码: 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

我有一个配置文件。我想从该文件中获取特定值。这是我的密码:

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"
}
但问题是它总是打印空行,而不是我期望的值
localhost:8000

您需要输入Configuration.domain字段,以便json包可以看到它

将域字段重命名为域:

type Configuration struct {
  Domain string
}
如果json名称与字段名不同,也可以显式指定json名称:

type Configuration struct {
  Domain string `json:"domain_name"`
}