2

I'm trying to work out a bit of code to pull in config from a JSON file. When I attempt to build, I get this error

type ConfigVars is not an expression 

Below is the config and program code I'm trying to work with. Every example I've found so far is similar to the below code. Any suggestion of what I'm doing incorrectly?

-- 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()) } } 

2 Answers 2

8

What you're doing there is trying to pass a pointer to the ConfigVars type (which obviously doesn't really mean anything). What you want to do is make a variable whose type is ConfigVars and pass a pointer to that instead:

var cfg ConfigVars err = jsonParser.Decode(&cfg) ... 
Sign up to request clarification or add additional context in comments.

1 Comment

Just to elaborate a bit more, the Decode method in your jsonParser object is expecting a pointer (memory address) of the ConfigVars instance to write output to. ConfigVars is a type, meaning it will only be allocated a memory address once an instance of it is created by means of a variable definition. i.e. var cfg ConfigVars
0

For others who come onto this problem, you may find that you've forgotten to initialize the variable during assignment using the := operator, as described in Point 3 at the end of this GoLang tutorial.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.