Nested structs on GAE datastore using Go

后端 未结 2 886
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-18 08:28

I\'m trying to figure out how to get nested structs to work with GAE datastore using Go. I know the datastore doesn\'t specifically support nested structs. I need to find a

2条回答
  •  轮回少年
    2021-01-18 09:23

    The python runtime has the ndb library which supports nested structures like this. Go does not, so I can think of two solutions:

    1. In the POST kind, have a user field that is a key, referencing a USER kind with the necessary fields. Requires two fetches and roundtrips.
    2. Make a user field in the POST kind that is a blob. The blob is a string that is [de]serialized in go. This means you can't search or filter on any of the user data, but it also allows you to store everything in one entity.

    You should use these based on the needs of your app. If you need users to be a real thing, use 1. If users aren't objects you need to work with (i.e., just data to display), you can use 2.

提交回复
热议问题