Skip to content

Instantly share code, notes, and snippets.

@stnc
Created July 2, 2023 09:15
Show Gist options
  • Save stnc/6d42c23d45add633262e06009820abd0 to your computer and use it in GitHub Desktop.
Save stnc/6d42c23d45add633262e06009820abd0 to your computer and use it in GitHub Desktop.
Embed map in JSON output for struct
type Response struct {
Text string `json:"text,omitempty"`
SessionAttributes map[string]any `json:"sessionAttributes,omitempty"`
}
func main() {
var renderA Response
renderA.Text = "dsds"
renderA.SessionAttributes = make(map[string]interface{}) //https://dev.to/rytsh/embed-map-in-json-output-5dnj
renderA.SessionAttributes["read"] = true
renderA.SessionAttributes["category"] = true
empJSON, err := json.MarshalIndent(renderA, "", " ")
if err != nil {
log.Fatalf(err.Error())
}
fmt.Printf("MarshalIndent funnction output\n %s\n", string(empJSON))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment