我正在尝试编写一些代码以从 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/