Making a post call with multiple instances of the same struct

163 views
Skip to first unread message

G K

unread,
Aug 5, 2024, 9:55:12 AM8/5/24
to golang-nuts
Hi,
   I'm trying to create an HTTP rest api end where a POST method will be able to accept and deserialize (unmarshall) a same data structre into numerous instances. For the sake of example, lets say I have json struct:
type Lock struct
{
   Key uint      `json:"key"`
}
Nom Im trying to post unspecified amount of keys. I tried defying the type as a slice but unmarshall function would not work. Does anyone have any ideas how to go about this?
Thanks,
GK

Ian Lance Taylor

unread,
Aug 5, 2024, 12:37:26 PM8/5/24
to G K, golang-nuts
Using a slice sounds like the right thing to do. What exactly did you
try and what exactly happened?

Ian

G K

unread,
Aug 5, 2024, 2:32:46 PM8/5/24
to golang-nuts
I defined a global variable of the struct kind "Lock" as a slice - var Lock []models.Lock
But then every time I tried to unmarshal the data from the post call using the the reference to &Lock, I was getting an error message that function couldn't unmarshal the object into a Go value. It unmarshals fine you define the var Lock model.Lock as just a struct.

Ian Lance Taylor

unread,
Aug 5, 2024, 2:51:23 PM8/5/24
to G K, golang-nuts
On Mon, Aug 5, 2024 at 11:32 AM G K <gku...@gmail.com> wrote:
>
> I defined a global variable of the struct kind "Lock" as a slice - var Lock []models.Lock
> But then every time I tried to unmarshal the data from the post call using the the reference to &Lock, I was getting an error message that function couldn't unmarshal the object into a Go value. It unmarshals fine you define the var Lock model.Lock as just a struct.

Thanks, but rather than rephrasing into English, show us exactly what
you did, and exactly what happened. Show us the exact code. Tell us
the exact error message. Thanks.

Ian


> On Monday, August 5, 2024 at 11:37:26 AM UTC-5 Ian Lance Taylor wrote:
>>
>> On Mon, Aug 5, 2024 at 6:55 AM G K <gku...@gmail.com> wrote:
>> >
>> > I'm trying to create an HTTP rest api end where a POST method will be able to accept and deserialize (unmarshall) a same data structre into numerous instances. For the sake of example, lets say I have json struct:
>> > type Lock struct
>> > {
>> > Key uint `json:"key"`
>> > }
>> > Nom Im trying to post unspecified amount of keys. I tried defying the type as a slice but unmarshall function would not work. Does anyone have any ideas how to go about this?
>>
>> Using a slice sounds like the right thing to do. What exactly did you
>> try and what exactly happened?
>>
>> Ian
>
> --
> 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...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/aab40be8-a26f-49f4-b762-d8527963ddcen%40googlegroups.com.

Marcello H

unread,
Aug 6, 2024, 3:14:30 AM8/6/24
to golang-nuts
Are you sure, when looking at the incoming message, that it is a json holding multiple "locks"?
If so, then you could make a small program just to unmarshall that , just to see what happens.

If you adjust the following code to what you want, you make it easier for us to understand:
https://go.dev/play/p/D_SSq2F_5hM
Op maandag 5 augustus 2024 om 20:51:23 UTC+2 schreef Ian Lance Taylor:
Reply all
Reply to author
Forward
0 new messages