Wednesday, March 15, 2023
HomeGolangAligning embedded kinds - Obtaining Assistance

Aligning embedded kinds – Obtaining Assistance

Below is my code

 bundle primary

import (
" encoding/json".
" fmt".

kind Information struct {
ID string 'json:" id"'.

kind expanded struct {
tn map[string] string.

func primary() {
b, _:= json.Marshal( prolonged {
Information: Information {
ID: "foo",.
tn: map[string] string {
" examination": "bar",.
} ).

fmt.Println( string( b)).

This is what I am obtaining

 {"id":" foo"}

And also I wish to obtain this

 {"id":" foo", "examination":" bar"}

Many thanks for your assistance.

json.Marshal just aligns public struct participants: Go Play Ground – The Go Programs Language

2 Sorts

I do not wish to publish TN in the raw message. Your code prints {"id":" foo"," TN": {"examination":" bar"}} as well as I desire {"id":" foo"," examination":" bar"}

You desire custom-made marshaling. Create a MarshalJSON() ([] byte, mistake) technique for your Prolonged kind. json bundle – encoding/json – Go Bundles

1 Like

tn requires to be altered to Tn to ensure that it is exported.
You can not marshall exclusive areas.


Most Popular

Recent Comments