填充包含切片的结构

时间:2018-08-19 10:08:56

标签: go

我正在尝试收集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)

2 个答案:

答案 0 :(得分:1)

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

您的结构声明略有偏离,例如,Page有a pointer to a slice of Post values,您可能想要a slice of Post pointers,因为这通常是人们使用切片的方式。您的声明只需要在类型旁边放置*,而不是[],然后您的创建代码就可以使用。

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
}

答案 1 :(得分:0)

您已将某些字段类型声明为指针到切片,但是您正在向他们提供类型为 slice-of-pointers 的值。

例如,给定字段Comments *[]Comment,您将像这样初始化其值:

Comments: &[]Comment{},

更多替代方法请参见此处:https://play.golang.org/p/l9HQEGxE5MP


如果已知元素类型(即它不是接口),则在切片,数组和映射中也是如此,您可以从元素的初始化中省略类型,而只需使用花括号,因此代码如下:

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

可以更改为:

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