C#/java Programmer to Go/golang Programmer's Notes (Day 05)

Source: Internet
Author: User

-----------------------------------------Error usage (Exception handling)

// Grammar   (normal error) " Errors "  int, err error) {    = errors. New (" custom error message ")}


-----------------------------------------Panic use (Exception handling)

// Grammar   (Fatal error) Panic (" custom error message "//  Direct Interrupt Application //  e.g. array out of bounds, NULL pointer exception is implemented inside go Panic function


-----------------------------------------Recover use (Exception handling)

// Grammar      (Recovery error) // recover must be placed behind the defer to use defer func () {    //  recover ()    if err: = Recover (); Err! = Nil {    //  produced a panic anomaly         FMT. Println (Recover ())    }} ()    //  Note: The defer method executes after completion, () calls the anonymous function by default

-----------------------------------------string Processing

//   base operation://Contains/join/index/repeat/repalce/split/trim/fiel   DS//  conversion://  append/format/parse


-----------------------------------------regexp Regular Expressions

Import"RegExp"Func Main () {buf:="0.2 456 asdx 1.233 7.8.99 Aloong 6.66"Reg:= RegExp. Mustcompile (' \d+\.\d ')ifReg = =Nil {//... error        return} Result:= Reg. Findallstring (BUF,-1)    //Print Resultresult = Reg. Findadllstringsubmatch (BUF,-1)}


-----------------------------------------JSON usage (struct to JSON)

Import ("Encoding/json"    "FMT")//convert JSON based on struct bodyType ITstruct{ Companystring                /*//Struct_tag use, Syntax: Company string ' JSON: ' Company ' '///two encoding company string ' JSON: '-' '//-means that the field does not output to the screen IsOK BOOL ' JSON: ', string '//Turn string*/subjects []stringIsOKBOOLPrice Float64}func Main () {s:= it{"it", []string{"C #","Java","python","Go"},false,666.66}        //buf, err: = json. Marshal (s)BUF, err: = json. Marshal (s),""," ")//format Encoding    ifErr! =Nil {//Print Err        return} fmt. Println (string(BUF))}


Use of-----------------------------------------JSON (map to JSON)

Func Main () {    m:= Make (map[string]interface1//  interface{} Universal data Type    m["company""loongsoft "        result, err: "" " " )    //  println (String (Result))}


-----------------------------------------JSON usage (JSON to struct)

Func Main () {    var  tmp IT    json. UNMARSHLL ([]byte(jsonvalue), &tmp)    //  print tmp}


-----------------------------------------IO Operation files

// Create/newfile/open/openfile // .. documents

Related Article

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.