google-app-engine - GoLang App Engine 结构名称

标签 google-app-engine struct go

我最近在使用 AppEngine,遇到了以下代码的问题,其中写入数据存储的唯一值似乎是日期。我花了一段时间思考这个问题,发现只需将结构中的第一个字符大写即可修复它!

我想知道是否有其他人遇到过这个问题并知道为什么我无法在数据存储的 Golang 结构中使用小写成员名称?我认为这可能是 Google AppEngine 处理结构写入方式的一个错误。


package main

import (


/* This is the problem struct */
type storeVal struct {
    firstName    string  //FirstName works
    lastName     string  //LastName works
    email        string  //Email works
    password     string  //Password works
    Date         time.Time

func init() {
    http.HandleFunc("/", handle)
    http.ListenAndServe(":8080", nil)

func handle(w http.ResponseWriter, r *http.Request) {
    c := appengine.NewContext(r)

    /* Write values to DataStore */
    e1 := storeVal{
        firstName:      "Bob",                 //FirstName works
        lastName:       "Smith",               //lastName works
        email:          "<a href="" class="__cf_email__" data-cfemail="92f0fdf0bce1fffbe6fad2e6f7e1e6bcf1fdff" rel="noreferrer noopener nofollow">[email protected]</a>",  //Email works
        password:       "password!",           //Password works
        Date:           time.Now(),

    key := datastore.NewIncompleteKey(c, "storeVal", nil)
    _, err := datastore.Put(c, key, &e1)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    fmt.Fprintf(w, "successfully added {firstName: '%s', lastName: '%s', email: '%s', password: '%s'}", e1.firstName, e1.lastName,, e1.password)

    /* Read values back in. */
    q := datastore.NewQuery("storeVal").
            Filter("firstName =", "Bob").
            Filter("lastName =", "Smith").
    var storeVals []storeVal
    _, err2 := q.GetAll(c, &storeVals)
    if err2 != nil {
        http.Error(w, err2.Error(), http.StatusInternalServerError)
    if (len(storeVals) == 0) {
        fmt.Printf("No results.");
    e2 := storeVals[0]
    fmt.Fprintf(w, "successfully grabbed {firstName: '%s', lastName: '%s', email: '%s', password: '%s', uaprem: '%s'}", e2.firstName, e2.lastName,, e2.password)



要导出,您确实需要大写的字段名称。 This可以帮助解释...它与 JSON 相关,但它适用于此处,因为这两种情况都需要从结构中导出数据。

这来自Eager blog post on Go and JSON ,我发现这很有帮助:

The Field Name

As you might know, Go requires all exported fields to start with a capitalized letter. It’s not common to use that style in JSON however. We use the tag to let the parser know where to actually look for the value.

You can see an example of that in the example above, but as a refresher this is what it looks like:

type MyStruct struct {
    SomeField string `json:"some_field"`

关于google-app-engine - GoLang App Engine 结构名称,我们在Stack Overflow上找到一个类似的问题:


python - GAE python : Process terminated because the backend was stopped

google-app-engine - Google App Engine、Eclipse、SDK 不可用?

c - C 中结构的逻辑运算符

c - 从指针传递的结构显示错误的值

http - net/http 设置自定义记录器

python - Google Apps Engine 数据存储区搜索

google-app-engine - 如何删除 GAE Search API 中的搜索索引

c++ - 通过一个套接字发送和接收两个结构

go - 如何防止负整数被视为简写标志

用于构建 API 的类似 Express 的框架