Appengine Go开发服务器找不到模板包

发布于 2024-12-10 11:08:47 字数 2327 浏览 0 评论 0原文

我正在尝试执行此处找到的 go appengine 教程,但我可以未完成导入模板库的示例。这是我正在尝试的示例代码:

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,如果这有帮助的话。

I'm attempting to do the go appengine tutorial found here, but I can't complete the example that imports the template library. This is the example code I am trying:

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>
`

The error I am receiving is:

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

My app.yaml is this:

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

handlers:
- url: /.*
  script: _go_app

I've tried modifying the dev_appserver.py EXTRA_PATHS list to include the system version of Go's libraries, since I noticed the appengine lib folder did not include a template library, but to no avail. Here is my current EXTRA_PATHSwith my changes being the last two entries:

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')
]

At this point, I'm not really sure how to proceed. I can't seem to find anywhere online that mentions a similar problem. I'm using the 64bit linux version of the appengine Go SDK, and my OS is Arch Linux, if that is any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

走过海棠暮 2024-12-17 11:08:47

从 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}

The sample is outdated starting with the SDK 1.5.5 update.

Now it should look more or less like this:

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>`

Notice the difference in call initializing var signTemplate and the template parameter in the signTemplateHTML variable, {{.|html}} instead of {@|html}.

攒眉千度 2024-12-17 11:08:47

Go 模板包最近已被重写。尝试导入“旧/模板”

2011-08-17(r60 的基础)

本周包含一些包重新洗牌。 http 和的用户
模板包可能会受到影响。

本周将模板包替换为 exp/template。这
原始模板包仍然可以作为 old/template 使用。这
旧的/模板包已被弃​​用,并将在某个时候被删除
将来。 Go 树已更新以使用新模板
包裹。我们鼓励旧模板包的用户切换到
新的。使用 template 或 exp/template 的代码需要
将其导入行分别更改为“old/template”或“template”。

The Go template package has recently been rewritten. Try importing "old/template".

2011-08-17 (base for r60)

This weekly contains some package re-shuffling. Users of the http and
template packages may be affected.

This weekly replaces the template package with exp/template. The
original template package is still available as old/template. The
old/template package is deprecated and will be removed at some point
in the future. The Go tree has been updated to use the new template
package. We encourage users of the old template package to switch to
the new one. Code that uses template or exp/template will need to
change its import lines to "old/template" or "template", respectively.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文