为什么在Google应用引擎中无法使用fmt.Println函数?

huangapple go评论93阅读模式
英文:

Why doesn't fmt.Println work in Google app engine

问题

我使用Google App Engine和Golang构建了一个简单的Web应用程序。在下面的代码中,我使用了两次fmt.Println来打印一些调试信息。我在运行应用程序时没有任何问题,除了终端上没有打印任何内容。

func HomeHandler(w http.ResponseWriter, r *http.Request) {
    c := appengine.NewContext(r)
    q := datastore.NewQuery("Post").Ancestor(goblogKey(c)).Order("-CreatedOn").Limit(10)

    //posts := make([]entity.Post, 0, 10)
    var posts []entity.Post

    if _, err := q.GetAll(c, &posts); err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }

    fmt.Println(string(len(posts)) + "...........")

    postList := []dto.Post{}

    for _, val := range posts {
        newpost := dto.Post{
            Post:     val,
            BodyHTML: template.HTML(val.Body),
        }

        fmt.Println(val.Title)

        postList = append(postList, newpost)
    }

    page := dto.PageData{Title: "Home", Posts: postList}
    templates.ExecuteTemplate(w, "index", page)
}
英文:

I built a simple web app using google app engine and golang. in code below, I use fmt.Println twice to print out somehting for debugging purpose. I have no problem running the app. everything works except nothing print out on the terminal.

func HomeHandler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	q := datastore.NewQuery("Post").Ancestor(goblogKey(c)).Order("-CreatedOn").Limit(10)

	//posts := make([]entity.Post, 0, 10)
	var posts []entity.Post

	if _, err := q.GetAll(c, &posts); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	fmt.Println(string(len(posts)) + "...........")

	postList := []dto.Post{}

	for _, val := range posts {
		newpost := dto.Post{
			Post:     val,
			BodyHTML: template.HTML(val.Body),
		}

		fmt.Println(val.Title)

		postList = append(postList, newpost)
	}

	page := dto.PageData{Title: "Home", Posts: postList}
	templates.ExecuteTemplate(w, "index", page)
}

答案1

得分: 10

在真实的App Engine环境中,你无法看到任何输出到标准输出(stdout)的内容。
App Engine的上下文提供了一种记录日志的方式(你可以在App Engine管理页面和控制台播放器中查看)。

func HomeHandler(w http.ResponseWriter, r *http.Request) {
    c := appengine.NewContext(r)
    c.Debugf("The message: %s", "foo")
    ...

阅读更多:https://developers.google.com/appengine/docs/go/reference#Context

英文:

In the real appengine enviroment you can't se anything output to stdout.
Appengine context give you away to log (that you can check in you appengine admin's page and in console playground).

func HomeHandler(w http.ResponseWriter, r *http.Request) {
    c := appengine.NewContext(r)
    c.Debugf("The message: %s", "foo")
    ...

Read more: https://developers.google.com/appengine/docs/go/reference#Context

答案2

得分: 0

标准输入/输出或错误用于与开发者使用的应用服务器进行通信。在生产系统中,使用标准输入/输出没有意义。在生产系统中,日志用于跟踪结果。在应用引擎中存在一些限制,例如 fmt、socket 等。

在远程服务器上测试或运行程序时,最好使用日志。

英文:

standard i/o Or Error is used communicate with app server used by the devleoper. In production system there's no meaning of using standard i/o. In production systems log is used to track the results. In app engine there's some limitations. like fmt, socket etc.

Its always better to use log when testing or running program in remote server.

huangapple
  • 本文由 发表于 2014年6月11日 11:25:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/24154156.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定