gpt4 book ai didi

go - 填充包含 slice 的结构

转载 作者:数据小太阳 更新时间:2023-10-29 03:10:57 24 4
gpt4 key购买 nike

我正在努力学习 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

你能帮我理解真正的问题是什么吗?我正在观看视频教程。

编辑:根据 mktopriva 建议更新了代码

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

出现以下错误

../handler.go:45: cannot use Post literal (type *Post) as type Post in array or slice literal
../handler.go:50: cannot use *Post literal (type *Post) as type Post in array or slice literal
../handler.go:55: cannot use Comment literal (type *Comment) as type Comment in array or slice literal
../handler.go:60: invalid indirect of []Comment literal (type []Comment)
../handler.go:62: invalid indirect of []Post literal (type []Post)

最佳答案

@mkopriva 是正确的,但我猜这不是你想要的......

您的结构声明略有偏差,例如,Page 有一个指向 Post 值 slice 的指针,您可能想要一个 Post 指针 slice ,因为那样会通常人们如何使用 slice 。您的声明只需要将 * 放在类型旁边,而不是 [] 然后您的创建代码就可以工作了。

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
}

关于go - 填充包含 slice 的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51916592/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com