vendredi 26 novembre 2021

API service - an array of struct grows bigger and bigger in each response

I have an API service written in Go which returns some kind of JSON. The json contains primitive types as well as nested structures and, importantly, arrays.

There's no database or any storage involved here.

I use only well known standard libraries such as "encoding/json", "net/http" and others.

I've noticied that in each response the json keys with type array will grow bigger and bigger, and as a result there'll appear more and more duplicates as I make requests.

The code is large, therefore I won't post it here.

I use the standard way of appending elements to an array

newElem := createNewElem(1, 2, 3)
retStruct.MyArrayElements1 = append(retStruct.MyArrayElements1, newElem)

How is it possible that the state of the array keys is preserved between requests in "net/http"?

Is this the normal standard behaviour of

a) append() function

b) arrays in general

c) "net/http" package

?

And, how to fix this? I want a new empty array in each request.




Aucun commentaire:

Enregistrer un commentaire