如何使用多于1行定义结构?
type Page struct {
Title string
ContentPath string
}
//this is giving me a syntax error
template := Page{
Title: "My Title",
ContentPath: "/some/file/path"
}
Run Code Online (Sandbox Code Playgroud)
sbe*_*rry 13
您需要用逗号结束所有行.
这是因为分号是如何自动插入的.
http://golang.org/ref/spec#Semicolons
您当前的代码最终结果如下
template := Page{
Title: "My Title",
ContentPath: "/some/file/path";
};
Run Code Online (Sandbox Code Playgroud)
添加逗号可以删除不正确的分号,但也可以在以后添加新项目时更轻松,而不必记住添加上面的逗号.
您只是在第二个字段后缺少一个逗号:
template := Page{
Title: "My Title",
ContentPath: "/some/file/path",
}
Run Code Online (Sandbox Code Playgroud)