json - 戈朗 : Type [type] is not an expression; json config parsing

标签 json go struct compiler-errors

我正在尝试编写一些代码以从 JSON 文件中提取配置。 当我尝试构建时,出现此错误

type ConfigVars is not an expression

下面是我尝试使用的配置和程序代码。到目前为止我发现的每个示例都类似于下面的代码。对我做错了什么有什么建议吗?

-- Config File
{"beaconUrl":"http://test.com/?id=1"}

-- Program Code
package main

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


type ConfigVars struct {
    BeaconUrl   string
}


func main() {
    configFile, err := os.Open("config.json")
    defer configFile.Close()
    if err != nil {
        fmt.Println("Opening config file", err.Error())
    }

    jsonParser := json.NewDecoder(configFile)
    if err = jsonParser.Decode(&ConfigVars); err != nil {
        fmt.Println("Parsing config file", err.Error())
    }
}

最佳答案

您在那里所做的是尝试将指针传递给 ConfigVars 类型(这显然没有任何意义)。你想要做的是创建一个类型为 ConfigVars 的变量并传递一个指向它的指针:

var cfg ConfigVars
err = jsonParser.Decode(&cfg)
...

关于json - 戈朗 : Type [type] is not an expression; json config parsing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35787463/

相关文章:

c - c中的输入输出结构函数错误

c++ - 结构错误 : Request for member

c - 如何比较结构和指针

Python Pyramid 解析 json

ios - JSON - 将 JSON 数据提取到数据模型中

json - 如何使用 argonaut 解析可选的自定义字段?

java - JSONRPC Java 实现

http - Golang 多次读取请求正文

google-app-engine - 如何在 App Engine 上部署标准 Go 应用程序

Go protobuf 包冲突