【问题标题】:c.Infof undefined (type context.Context has no field or method Infof) google.golang.org/appengine/log errorc.Infof 未定义(类型 context.Context 没有字段或方法 Infof)google.golang.org/appengine/log 错误
【发布时间】:2015-03-20 14:25:31
【问题描述】:

在 Go Runtime 中,我使用 c.Infof 方法记录消息,但编译失败并出现以下错误 c.Infof 未定义(类型 context.Context 没有字段或方法 Infof)。 错误清楚地表明从 c := appengine.NewContext(r) 返回的应用引擎上下文是 context.Context 类型 并且它没有方法 c.Infof。但与此相反,https://godoc.org/google.golang.org/appengine/log 中的文档表明此方法存在。还有一点需要注意,该方法存在于“appengine”(导入“appengine”)包返回的上下文中,而新包返回的上下文中似乎不存在该方法google.golang.org/ appenginec.Infof 与包 "google 返回的 context.Context 类型的新上下文等效.golang.org/appengine" ?

【问题讨论】:

    标签: google-app-engine go


    【解决方案1】:

    包文档中的示例不正确。

    使用 log 包函数写入 App Engine 日志。这是更正后的示例:

    c := appengine.NewContext(r)
    query := &log.Query{
        AppLogs:  true,
        Versions: []string{"1"},
    }
    
    for results := query.Run(c); ; {
        record, err := results.Next()
        if err == log.Done {
           log.Infof(c, "Done processing results")
           break
        }
        if err != nil {
            log.Errorf(c, "Failed to retrieve next log: %v", err)
            break
        }
        log.Infof(c, "Saw record %v", record)
    }
    

    包文档中的示例是从 App Engine Classic 包中复制的,但未更新以使用新功能。我建议将此情况报告给 App Engine 团队。

    【讨论】:

    • 哇!!很多程序员都会受到文档中这个错误的影响!谢谢你:)
    猜你喜欢
    • 2021-02-22
    • 2017-08-03
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 2017-09-21
    • 1970-01-01
    • 2020-12-24
    相关资源
    最近更新 更多