看看在here发现的这个片段
import ( "encoding/xml" "fmt" "os" ) func main() { type Address struct { City, State string } type Person struct { XMLName xml.Name `xml:"person"` Id int `xml:"id,attr"` FirstName string `xml:"name>first"` LastName string `xml:"name>last"` Age int `xml:"age"` Height float32 `xml:"height,omitempty"` Married bool Address Comment string `xml:",comment"` } v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42} v.Comment = " Need more details. " v.Address = Address{"Hanga Roa", "Easter Island"} enc := xml.NewEncoder(os.Stdout) enc.Indent(" ", " ") if err := enc.Encode(v); err != nil { fmt.Printf("error: %v\n", err) } }
我可以理解在struct Person
,它有一个名为Id
的var,它是int
类型,但是这个东西怎么样
xml:"person"
after int? What does it mean? Thanks.
它是一个struct标签。库使用这些来为带有额外信息的结构域注释;在这种情况下,模块encoding/xml使用这些struct标签来表示哪些标签对应于struct字段。
这意味着该变量将出现在Person示例的名称中
type sample struct {
dateofbirth string `xml:"dob"`
}
In the above example, the field 'dateofbirth' will present in the name of 'dob' in the XML.
你会在go结构中经常看到这种符号。