我正在尝试收集 Go 的基础知识。
我正在尝试在 golang 中呈现一个带有结构预填充值的模板。但没有运气
func ServeIndex(w http.ResponseWriter, r *http.Request) {
p := &Page{
Title: " Go Project CMS",
Content: "Welcome to our home page",
Posts: []*Post{
&Post{
Title: "Hello World",
Content: "Hello, World Thanks for coming to this site",
DatePublished: time.Now(),
},
&Post{
Title: "A Post with comments",
Content: "Here is the controversial post",
DatePublished: time.Now(),
Comments: []*Comment{
&Comment{
Author: "Sathish",
Comment: "Nevermind, I guess",
DatePublished: time.Now().Add(-time.Hour / 2),
},
},
},
},
}
Tmpl.ExecuteTemplate(w, "page", p)
}
这是我的结构定义
import (
"html/template"
"time"
)
// Tmpl is exported and can be used by other packages
var Tmpl = template.Must(template.ParseGlob("../templates/*"))
type Page struct {
Title string
Content string
Posts *[]Post
}
type Post struct {
Title string
Content string
DatePublished time.Time
Comments *[]Comment
}
type Comment struct {
Author string
Comment string
DatePublished time.Time
}
当我尝试通过 main.go 文件运行此代码时,出现以下错误
../handler.go:60: cannot use []*Comment literal (type []*Comment) as type *[]Comment in field value
../handler.go:62: cannot use []*Post literal (type []*Post) as type *[]Post in field value
你能帮我理解真正的问题是什么吗?我正在观看视频教程。
ABOUTYOU
慕桂英3389331
相关分类