Not directly, but you can implement sth akin: https://go.dev/play/p/vN2uAp9rRpp With reflection, you can build a helper that uses the exposed MarshalInto on types that implement it, and falls back to Marshal otherwise.
jerome....@gmail.com a következőt írta (2022. december 10., szombat, 8:31:26 UTC+1): > Hello, > I would like to know if there is elegant way to json encode very long > field (with io.Reader)? > > For example: (play ground https://go.dev/play/p/QxlbNMy3ERK ) > ``` > func TestJSONStreamBytes(t *testing.T) { > type MyStruct struct { > Reader1 io.Reader `json:"field1"` > Reader2 io.Reader `json:"field2"` > } > > out := &bytes.Buffer{} > > err := json.NewEncoder(out).Encode(MyStruct{ > Reader1: strings.NewReader("this stream can be very long from S3 > Bucket for example"), > Reader2: strings.NewReader("this stream can be very long from > another S3 Bucket for example"), > }) > if err != nil { > panic(err) > } > > t.Log(out.String()) > } > ``` > It returns prog.go:29: {"field1":{},"field2":{}} > > But I want base64encoded response managed as stream mode (i.e. I don't > want to load the data in our memory) like : > > > {"field1":"dGhpcyBzdHJlYW0gY2FuIGJlIHZlcnkgbG9uZyBmcm9tIFMzIEJ1Y2tldCBmb3IgZXhhbXBsZQ==","field2":"dGhpcyBzdHJlYW0gY2FuIGJlIHZlcnkgbG9uZyBmcm9tIGFub3RoZXIgUzMgQnVja2V0IGZvciBleGFtcGxl"} > > > Thank in advance, > Jérôme > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/b14b47af-fea8-4c21-8dbb-013ee30201b3n%40googlegroups.com.