Appengine Go开发服务器无法找到模板包。

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

Appengine Go development server can't find the template package

问题

我正在尝试完成在这里找到的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>
`

我收到的错误是:

编译错误:
    /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 (
    &quot;fmt&quot;
    &quot;http&quot;
    &quot;template&quot;
)

func init() {
    http.HandleFunc(&quot;/&quot;, root)
    http.HandleFunc(&quot;/sign&quot;, sign)
}

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

const guestbookForm = `
&lt;html&gt;
  &lt;body&gt;
    &lt;form action=&quot;/sign&quot; method=&quot;post&quot;&gt;
      &lt;div&gt;&lt;textarea name=&quot;content&quot; rows=&quot;3&quot; cols=&quot;60&quot;&gt;&lt;/textarea&gt;&lt;/div&gt;
      &lt;div&gt;&lt;input type=&quot;submit&quot; value=&quot;Sign Guestbook&quot;&gt;&lt;/div&gt;
    &lt;/form&gt;
  &lt;/body&gt;
&lt;/html&gt;
`

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

var signTemplate = template.MustParse(signTemplateHTML, nil)

const signTemplateHTML = `
&lt;html&gt;
  &lt;body&gt;
    &lt;p&gt;You wrote:&lt;/p&gt;
    &lt;pre&gt;{@|html}&lt;/pre&gt;
  &lt;/body&gt;
&lt;/html&gt;
`

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, &#39;lib&#39;, &#39;antlr3&#39;)
  ,os.path.join(DIR_PATH, &#39;lib&#39;, &#39;django_0_96&#39;)
  ,os.path.join(DIR_PATH, &#39;lib&#39;, &#39;fancy_urllib&#39;)
  ,os.path.join(DIR_PATH, &#39;lib&#39;, &#39;ipaddr&#39;)
  ,os.path.join(DIR_PATH, &#39;lib&#39;, &#39;protorpc&#39;)
      ,os.path.join(DIR_PATH, &#39;lib&#39;, &#39;webob&#39;)
  ,os.path.join(DIR_PATH, &#39;lib&#39;, &#39;yaml&#39;, &#39;lib&#39;)
  ,os.path.join(DIR_PATH, &#39;lib&#39;, &#39;simplejson&#39;)
  ,os.path.join(DIR_PATH, &#39;lib&#39;, &#39;google.appengine._internal.graphy&#39;)
  ,os.path.join(&#39;usr&#39;, &#39;lib&#39;, &#39;go&#39;, &#39;lib&#39;)
  ,os.path.join(&#39;usr&#39;, &#39;lib&#39;, &#39;go&#39;, &#39;pkg&#39;, &#39;linux_amd64&#39;)
]

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.

答案1

得分: 3

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




`

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


You wrote:

{{.|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 (
	&quot;fmt&quot;
	&quot;http&quot;
	&quot;template&quot;
)

func init() {
	http.HandleFunc(&quot;/&quot;, root)
	http.HandleFunc(&quot;/sign&quot;, sign)
}

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

const guestbookForm = `
&lt;html&gt;
 &lt;body&gt;
  &lt;form action=&quot;/sign&quot; method=&quot;post&quot;&gt;
	&lt;div&gt;&lt;textarea name=&quot;content&quot; rows=&quot;3&quot; cols=&quot;60&quot;&gt;&lt;/textarea&gt;&lt;/div&gt;
	&lt;div&gt;&lt;input type=&quot;submit&quot; value=&quot;Sign Guestbook&quot;&gt;&lt;/div&gt;
  &lt;/form&gt;
 &lt;/body&gt;
&lt;/html&gt;
`

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

var signTemplate = template.Must(template.New(&quot;SignIn&quot;).Parse(signTemplateHTML))

const signTemplateHTML = `
&lt;html&gt;
 &lt;body&gt;
  &lt;p&gt;You wrote:&lt;/p&gt;
  &lt;pre&gt;{{.|html}}&lt;/pre&gt;
 &lt;/body&gt;
&lt;/html&gt;`

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

答案2

得分: 1

最近,Go的模板包已经被重写。尝试导入"old/template"。

2011-08-17 (r60的基础)

这个版本进行了一些包的重新整理。使用http和template包的用户可能会受到影响。

这个版本用exp/template替换了template包。原始的template包仍然可用作old/template。old/template包已被弃用,并将在将来的某个时候被移除。Go树已经更新为使用新的template包。我们鼓励使用旧template包的用户切换到新的包。使用template或exp/template的代码需要将其导入行更改为"old/template"或"template"。

英文:

The Go template package has recently been rewritten. Try importing &quot;old/template&quot;.

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.

huangapple
  • 本文由 发表于 2011年10月15日 08:40:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/7774953.html
匿名

发表评论

匿名网友

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

确定