去语法错误,不平衡的大括号

时间:2016-01-22 21:25:29

标签: go

当我尝试运行我的应用时,它说第32行有一个意外的},我已计算{},似乎它们是偶数但是当我删除f1,f2和p效果很好。我该如何解决这个问题?

package main

import (
    "fmt"
    "log"
    "net/http"
    "html/template"
)

type friend struct{
    fname   string
}

type person struct{
    username    string
    emails      []string
    friends     []*friend
}

func home(w http.ResponseWriter, r *http.Request){
    f1 := friend{fname: "minux.ma"}
    f2 := friend{fname: "xushiwei"}
    p := Person{
        userName: "Astaxie",
        emails: []string{"astaxie@beego.me", "astaxie@gmail.com"},
        friends: []*Friend{&f1, &f2}
    }

    t, err := template.ParseFiles("template.gtpl")
    if err != nil{
        fmt.Println(err)
    }

    t.Execute(w, nil)
}

func main(){

    http.HandleFunc("/", home)
    err := http.ListenAndServe(":8000", nil)
    if err != nil{
        log.Fatal("listen and serve:", err)
    }
}

1 个答案:

答案 0 :(得分:3)

出现意外},因为您在friends行的末尾错过了逗号

friends: []*Friend{&f1, &f2},

始终go fmt您的代码。 (或使用goimports)。它会给出错误的确切行和列:

  

在复合文字中的换行符之前缺少','