The structure of the Go language learning pen

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.