Code
package main
import(
"os"
"fmt"
"text/template"
)
type Person struct{
Name string
Age string
Title string
}
func main(){
t, err := template.ParseFiles("./index.html")
if err != nil{
fmt.Println("parse err: ", err)
return
}
p := Person{
Name: "Alex",
Age: "19",
Title: "这是一个网页"
}
if err != t.Execute(os.Stdout, p); err != nil{
fmt.Println("There was an error: ", err.Error())
}
}
报错:./main.go:24:36: syntax error: unexpected newline, expecting comma or }
原因在于结构体想要进行多行编写时,最后一个元素后面需要加上一个逗号
p := Person{
Name: "Alex",
Age: "19",
Title: "这是一个网页",
}
报错:./main.go:26:12: err != t.Execute(os.Stdout, p) evaluated but not used
已评估但未使用,语句错误
if err := t.Execute(os.Stdout, p); err != nil{
fmt.Println("There was an error: ", err.Error())
}