Chingiz Ochirov Chingiz Ochirov - 1 year ago 63
Python Question

How to use datastore GAE in Go when initially it was created in Python?

I have a datastore kind

which was created in Python, in this code do not iterate data
in Go (it's version 2):

type Items struct{
code string
date time.Time
name string
func getcode(w http.ResponseWriter, r *http.Request) {
code := mux.Vars(r)["code"]
fmt.Fprintf(w,"get code %v",code)

c := appengine.NewContext(r)
q := datastore.NewQuery("Items")

for t := q.Run(c); ; {
var x Items
key, err := t.Next(&x)

if err == datastore.Done {
if err != nil {
//serveError(c, w, err)
fmt.Fprintf(w, "Code=%v\n", x.code)

Answer Source

The Datastore package uses reflection to fill struct fields when reading an entity from the datastore. In Go struct fields whose name start with lowercase letter are not exported. Unexported fields cannot be set from packages other than the one they were defined in.

Only exported fields (that start with uppercase letters) can be stored in / retrieved from the datastore. You can use tags to tell what the name of the property is in the datastore in case it differs from the field's name. So you have to change your Items struct to this:

type Items struct {
    Code string    `datastore:"code"`
    Date time.Time `datastore:"date"`
    Name string    `datastore:"name"`