英文:
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 (
"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_PATHS
with 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.
答案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 (
"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}
.
答案2
得分: 1
最近,Go的模板包已经被重写。尝试导入"old/template"。
这个版本进行了一些包的重新整理。使用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 "old/template"
.
> 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论