-----------------------------------------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