Here's what I do to write a structure to a JSON file -
type userInputs struct {
...
}
userInputsJson := userInputs{}
// Fill userInputsJson
fp, err := os.Create(DFLT_INPUT_FILE)
if err != nil {
logger.Fatalf("Unable to create %v. Err: %v.", DFLT_INPUT_FILE, err)
return err
}
defer fp.Close()
encoder := json.NewEncoder(fp)
if err = encoder.Encode(userInputsJson); err != nil {
logger.Fatalf("Unable to encode Json file. Err: %v.", err)
return err
}
The output is a JSON file that's not very pleasing to the eye. I'd like to have it indented, I looked up online to find MarshalIndent that does it, but I'm not sure how to output that to a file. What am I missing?
Thank you!