看一下在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
的变量,它的类型为int
,但之后的内容
xml:"person"
诠释?这是什么意思?感谢。
答案 0 :(得分:3)
这是一个struct标签。库使用这些来为带有额外信息的结构域注释;在这种情况下,模块encoding/xml使用这些struct标签来表示哪些标签对应于struct字段。
答案 1 :(得分:0)
这意味着变量将以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结构中看到这种表示法。