语法错误:意外换行,期望逗号或)



我得到错误语法错误:意外换行,期望逗号或)。我在golang新。我尝试新的代码,但同样的错误。有人能帮帮我吗?我的代码

package main
import "fmt"
import "net/http"
import "html/template"
import "path"
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
var filepath = path.Join("index.html")
var tmpl, err = template.ParseFiles(filepath)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

http.HandleFunc("/membuat-web-dengan-golang", func(w http.ResponseWriter, r *http.Request) {
var filepath = path.Join("membuat-web-dengan-golang.html")
var tmpl, err = template.ParseFiles(filepath)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("assets"))))
fmt.Println("server started at localhost:9000")
http.ListenAndServe(":9000", nil)
}

你好,你的HandleFunc函数缺少右括号。检查我添加到代码中的fmt.Println(tmpl)行。第一个右花括号}用于匿名函数,第二个右花括号)用于HandleFunc。

func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
var filepath = path.Join("index.html")
var tmpl, err = template.ParseFiles(filepath)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Println(tmpl)})

http.HandleFunc("/membuat-web-dengan-golang", func(w http.ResponseWriter, r *http.Request) {
var filepath = path.Join("membuat-web-dengan-golang.html")
var tmpl, err = template.ParseFiles(filepath)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Println(tmpl)})

http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("assets"))))
fmt.Println("server started at localhost:9000")
http.ListenAndServe(":9000", nil)
}

最新更新