4

Take a look at this snip found at 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)
 }
}

I can understand in the struct Person, It has a var called Id, which is of type int, but what about the stuff

xml:"person" 
after int? What does it mean? Thanks.

asked Sep 8, 2013 at 20:56

2 Answers 2

5

It's a struct tag. Libraries use these to annotate struct fields with extra information; in this case, the module encoding/xml uses these struct tags to denote which tags correspond to the struct fields.

answered Sep 8, 2013 at 21:47
Sign up to request clarification or add additional context in comments.

1 Comment

See go.dev/ref/spec#Struct_types for the primary source.
0

which mean that variable will present in the name of Person example

type sample struct {
 dateofbirth string `xml:"dob"`
}
In the above example, the field 'dateofbirth' will present in the name of 'dob' in the XML.

you will see this notation often in go struct.

answered Sep 7, 2018 at 7:09

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.