Go 多行结构定义

Go 多行结构定义,go,Go,如何使用多行定义结构 type Page struct { Title string ContentPath string } //this is giving me a syntax error template := Page{ Title: "My Title", ContentPath: "/some/file/path" } 第二个字段后缺少一个逗号: template := Page{ Title: "My Title", Cont

如何使用多行定义结构

type Page struct {
    Title string
    ContentPath string
}

//this is giving me a syntax error
template := Page{
    Title: "My Title",
    ContentPath: "/some/file/path"
}

第二个字段后缺少一个逗号:

template := Page{
    Title: "My Title",
    ContentPath: "/some/file/path",
}

您需要以逗号结束所有行

这是因为分号是如何自动插入的

您当前的代码以如下方式结束

template := Page{
    Title: "My Title",
    ContentPath: "/some/file/path";
};

添加逗号可以去掉不正确的分号,但也可以使以后添加新项目变得更容易,而不必记住在上面添加逗号。

谢谢,我试过了,认为它不起作用,结果发现这是我真正遇到的问题:为什么对这个问题投反对票?多行结构文字在Go中不是惯用的吗?可能是因为问题标题和问题正文都没有提到Go。只是一个标签。(这是一个真诚的猜测;我不是在讽刺)