google-app-engine - Appengine Go开发服务器找不到模板包

标签 google-app-engine templates go

我正在尝试执行 go appengine 教程,发现 here ,但我无法完成导入模板库的示例。这是我正在尝试的示例代码:

package hello

import (
    "fmt"
    "http"
    "template"
)

func init() {
    http.HandleFunc("/", root)
    http.HandleFunc("/sign", sign)
}

func root(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, guestbookForm)
}

const guestbookForm = `
<html>
  <body>
    <form action="/sign" method="post">
      <div><textarea name="content" rows="3" cols="60"></textarea></div>
      <div><input type="submit" value="Sign Guestbook"></div>
    </form>
  </body>
</html>
`

func sign(w http.ResponseWriter, r *http.Request) {
    err := signTemplate.Execute(w, r.FormValue("content"))
    if err != nil {
        http.Error(w, err.String(), http.StatusInternalServerError)
    }
}

var signTemplate = template.MustParse(signTemplateHTML, nil)

const signTemplateHTML = `
<html>
  <body>
    <p>You wrote:</p>
    <pre>{@|html}</pre>
  </body>
</html>
`

我收到的错误是:

Compile error:
    /home/habitue/Programming/GoExamples/hello/hello.go:36: undefined: template.MustParse

我的app.yaml是这样的:

application: helloworld
version: 1
runtime: go
api_version: 3

handlers:
- url: /.*
  script: _go_app

我尝试修改 dev_appserver.py EXTRA_PATHS 列表以包含 Go 库的系统版本,因为我注意到 appengine lib 文件夹不包含模板库,但无济于事。这是我当前的 EXTRA_PATHS,其中我的更改是最后两个条目:

EXTRA_PATHS = [
  DIR_PATH
  ,os.path.join(DIR_PATH, 'lib', 'antlr3')
  ,os.path.join(DIR_PATH, 'lib', 'django_0_96')
  ,os.path.join(DIR_PATH, 'lib', 'fancy_urllib')
  ,os.path.join(DIR_PATH, 'lib', 'ipaddr')
  ,os.path.join(DIR_PATH, 'lib', 'protorpc')
      ,os.path.join(DIR_PATH, 'lib', 'webob')
  ,os.path.join(DIR_PATH, 'lib', 'yaml', 'lib')
  ,os.path.join(DIR_PATH, 'lib', 'simplejson')
  ,os.path.join(DIR_PATH, 'lib', 'google.appengine._internal.graphy')
  ,os.path.join('usr', 'lib', 'go', 'lib')
  ,os.path.join('usr', 'lib', 'go', 'pkg', 'linux_amd64')
]

此时,我不太确定如何继续。我似乎无法在网上找到提到类似问题的任何地方。我正在使用 64 位 Linux 版本的 appengine Go SDK,我的操作系统是 Arch Linux,如果这有帮助的话。

最佳答案

从 SDK 1.5.5 更新开始,该示例已过时。

现在它应该看起来或多或少像这样:

package main

import (
    "fmt"
    "http"
    "template"
)

func init() {
    http.HandleFunc("/", root)
    http.HandleFunc("/sign", sign)
}

func root(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, guestbookForm)
}

const guestbookForm = `
<html>
 <body>
  <form action="/sign" method="post">
    <div><textarea name="content" rows="3" cols="60"></textarea></div>
    <div><input type="submit" value="Sign Guestbook"></div>
  </form>
 </body>
</html>
`

func sign(w http.ResponseWriter, r *http.Request) {
    err := signTemplate.Execute(w, r.FormValue("content"))
    if err != nil {
        http.Error(w, err.String(), http.StatusInternalServerError)
    }
}

var signTemplate = template.Must(template.New("SignIn").Parse(signTemplateHTML))

const signTemplateHTML = `
<html>
 <body>
  <p>You wrote:</p>
  <pre>{{.|html}}</pre>
 </body>
</html>`

请注意调用初始化 var signTemplate 和 signTemplateHTML 变量中的模板参数的区别,{{.|html}} 而不是 {@|html }.

关于google-app-engine - Appengine Go开发服务器找不到模板包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7774953/

相关文章:

google-app-engine - 如何使用电子邮件地址查询 Appengine 数据存储区

java - 由 : java. lang.ClassCastException 引起 : com. google.appengine.api.datastore.Text 无法转换为 java.lang.String

html - 像推特一样剪辑网页的边框/圆 Angular ?

c++ - C++ 模板声明中的范围和默认参数 : Clarifying Standardese

go - http.HandlerFunc 如何在没有明确传递的情况下获得响应编写器和请求?

google-app-engine - GAE 命名空间与线程安全一起使用是否安全?

java - Android 中的安全性 - Google 应用引擎系统

c++ - typedef 有效, 'using =' 无效

go - 编码/取消编码后本地时间损失 52 秒

go - 仅从 Go 中的函数引用获取接收器类型和方法名称