duaner1634 2011-10-15 00:40
浏览 61
已采纳

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

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.

  • 写回答

2条回答 默认 最新

  • dongzhan1878 2011-10-15 19:06
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大