About obtaining entities from Datastore after deployment to Golang GCP

Asked 1 years ago, Updated 1 years ago, 108 views

I'm sorry to ask this question for a beginner who just started studying Go, but if anyone knows it, please let me know.

What I want to do now is to get the entities stored in the GCP datastore, mold them into a JSON type, and respond.

The source will be as follows, but in the local environment, the entity can now be retrieved and the JSON molded one will be answered.
However, after deployment, if you try to get it, you can create a JSON container, but the contents are empty.

Because I am a beginner, I think there are many things that I can't do.If there are any missing sources, I will add them.

Thank you for your cooperation.

init.go

 funcinit(){

    http.HandleFunc("/test", GET)

}

test.go

package sample
import(
    "net/http"
    "os"
    "time"

    "encoding/json"
    "fmt"

    "google.golang.org/appengine"
    "google.golang.org/appengine/datastore"

)

// A structure specializing in this sample for the entity to be stored in the datastore
type ScheduleEntity structure {
    Schedule_id int`json: "schedule_id"`
    Inquiry_Interval int`json: "quiry_Interval"`
    Update_status int`json: "update_status"`
    Request_sounds_flg string `json: "request_sounds_flg"`
    Schedule_detail_id int`json: "schedule_detail_id"`
    End_interval int`json: "end_interval"`
    Sound_name string `json: "sound_name"`
    Content string `json: `content``
}

// Tag name for json response
type JsonScheduleEntity structure {
    Schedule_id int`json: "schedule_id"`
    Inquiry_Interval int`json: "quiry_interval"`
    Update_status int`json: "update_status"`
    Request_sounds_flg string `json: "request_sounds_flg"`
    Co[] JsonContents `json: "content_detail"`
}
type JsonContents structure {
    Schedule_detail_id int`json: "schedule_detail_id"`
    End_interval int`json: "end_interval"`
    Sound_name string `json: "sound_name"`
    Content string `json: `content``
}

func GET(whttp.ResponseWriter,r*http.Request){
    ifr.Method=="GET" {

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

        var Co[]JsonContents
        _,err —=q.GetAll(c,Co)
        // var Co[ ] JsonContents
        var de JsonScheduleEntity
        de. Co = Co

        if err!=nil{
            os.Stderr.Write([]byte("Error\n"))
            return
        }
        j: = json.NewEncoder(w).Encode(de)
        fmt.Fprint(w,j)
}

go google-app-engine

2022-09-30 21:21

1 Answers

var Co[]JsonContents
_,err —=q.GetAll(c,Co)

The GetAll argument must be passed with the pointer of slice, so

_,err:=q.GetAll(c, & Co)

Try as .

However, the two things I can't understand are the following.

In your local environment, you can now retrieve entities

After deployment, if you try to get it, you can create a JSON container, but the contents are empty

There should be errors in both local and production environments...


2022-09-30 21:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.