The structure method in go language
The case of struct names has different meanings: lowercase means not accessible by other packages
Package Mainimport ' FMT ' type Rect struct {width,height int}func (R *rect) area () int {return r.width*r.height;} Func Main () {r:=rect{width:10,height:10}fmt. Println (R.area ());rp:=&r; Fmt. Println (Rp.area ());}
Using structs to parse XML
First we look at the XML format, which is different for the attribute (attr) and node use.
Specific use of the following methods
<bulkPmMrDataFile> <fileheader fileformatversion= "1.0.3" reporttime= "2015-06-18t10:45:00.178+08:00" starttime= "2015-06-18t10:30:00.178+08:00" endtime= "2015-06-18t10:45:00.178+08:00" period= "0"/></ Bulkpmmrdatafile>
Package Ticketimport ("Path" "Io/ioutil" "FMT" "Encoding/xml" "OS") Type Bulkpmmrdatafilestruct{fileheader fileheader ' xml:"Fileheader"' ENB ENB ' xml:"ENB"'}type fileheaderstruct{fileformatversionstring' XML:"fileformatversion,attr"' Reporttimestring' XML:"reporttime,attr"' StartTimestring' XML:"starttime,attr"' EndTimestring' XML:"endtime,attr"' Periodstring' XML:"period"'}type ENBstruct{Idstring' XML:"id,attr"' Userlabelstring' XML:"userlabel,attr"' Measurement measurement ' xml:"Measurement"'}type measurementstruct{SMRstring' XML:"SMR"' Object object ' xml:"Object"'}type Objectstruct{Idstring' XML:"id,attr"' Mmeues1apidstring' XML:"mmeues1apid,attr"' Mmecodestring' XML:"Mmecode"' TimeStampstring' XML:"TimeStamp"' Vstring' XML:"v"'}func checkeror (err error) {ifErr! =Nil {fmt. fprintf (OS. Stderr,"Fatal Error:%s", Err. Error ()) OS. Exit (1)}}func Main () {filename:= path. Join ("input","xxxx. XML") content, err:=Ioutil. ReadFile (filename) fmt. PRINTLN (filename); if(err!=Nil) {FMT. Println ("Open File Err") Checkeror (err)}varresult Bulkpmmrdatafile Err=xml. Unmarshal (Content, &result)if(err!=Nil) {Checkeror (Err)} FMT. Println (" Here"); Fmt. Println ("Report Time :"+result. Fileheader.reporttime); Fmt. Println ("ENB ID object.id:"+result. ENB. MEASUREMENT.OBJECT.V);}
The structure of the Go language learning pen