-1

Goを勉強し始めたばかりの初心者の質問で恐縮ですが、もしご存知の方がいたらご教示いただきたく存じます。

今自分がやりたいことは、GCPのデータストアに格納されているエンティティを取得し、JSON型に成型して返答することです。

ソースが以下になりますが、ローカル環境では、これでエンティティの取得ができ、JSON型に成型されたものが返答されます。
しかし、デプロイ後はGETしようとすると、JSON型の入れ物は作成できますが、中身が空になってしまいます。

初心者のため、至らない点が多いかと思います。もし、足りないソース等ございましたら、追記させていただきます。

宜しくお願い致します。

init.go

func init() {

    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"

)

// 今回のサンプルに特化した、Datastoreに格納するエンティティ用の構造体
type ScheduleEntity struct {
    Schedule_id        int       `json:"schedule_id"`
    Inquiry_Interval   int       `json:"inquiry_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"`
}

// jsonレスポンスとして利用するためタグ名を指定
type JsonScheduleEntity struct {
    Schedule_id        int            `json:"schedule_id"`
    Inquiry_Interval   int            `json:"inquiry_interval"`
    Update_status      int            `json:"update_status"`
    Request_sounds_flg string         `json:"request_sounds_flg"`
    Co                 []JsonContents `json:"content_detail"`
}
type JsonContents struct {
    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(w http.ResponseWriter, r *http.Request) {
    if r.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)
}

1 件の回答 1

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

GetAllの引数にはsliceのポインタを渡す必要があるので、

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

として試してみてください。

ただ解せないのは下記2点ですね。

ローカル環境では、これでエンティティの取得ができ

デプロイ後はGETしようとすると、JSON型の入れ物は作成できますが、中身が空に

ローカル環境でもプロダクション環境でもエラーになるはずですが・・・

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。