Created
July 2, 2023 09:15
-
-
Save stnc/6d42c23d45add633262e06009820abd0 to your computer and use it in GitHub Desktop.
Embed map in JSON output for struct
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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