亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

填充包含切片的結構

填充包含切片的結構

Go
呼啦一陣風 2023-03-21 16:00:32
我正在嘗試收集 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 packagesvar 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你能幫我理解真正的問題是什么嗎?我正在觀看視頻教程。
查看完整描述

2 回答

?
ABOUTYOU

TA貢獻1812條經驗 獲得超5個贊

我猜這不是你想要的......


您的結構聲明略有偏離,例如,a pointer to a slice of Post values您可能想要 Page has ,a slice of Post pointers因為這通常是人們使用切片的方式。您的聲明只需要*類型旁邊的 put,而不是[]然后您的創建代碼將起作用。


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

}


查看完整回答
反對 回復 2023-03-21
?
慕桂英3389331

TA貢獻2036條經驗 獲得超8個贊

您將某些字段類型聲明為pointers-to-slices,但您向它們提供了slice-of-pointers類型的值。

例如,給定字段Comments *[]Comment,您可以像這樣初始化它的值:

Comments: &[]Comment{},

請參閱此處了解更多替代方案:https://play.golang.org/p/l9HQEGxE5MP


同樣在切片、數組和映射中,如果元素類型已知,即它不是接口,則可以在元素的初始化中省略類型,只使用花括號,因此代碼如下:

[]*Post{&Post{ ... }, &Post{ ... }}

可以更改為:

[]*Post{{ ... }, { ... }}


查看完整回答
反對 回復 2023-03-21
  • 2 回答
  • 0 關注
  • 159 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號