The use of the Go Language type conversion Library "github.com/demdxx/gocast"

Source: Internet
Author: User

first, Guide The library: go get github.com/demdxx/gocast

second, the test code:

 

//Main.gopackage Mainimport ("FMT"    "reflect"    "Github.com/demdxx/gocast") Type Accessregionstruct{region_id Int64 ' Tag:"RegionID"' provider_id Int64 ' tag:"ProviderID"' Region_namestring' Tag2"Regionname"' Sub_region_namesstringBilling_region_namestringDescriptionstring}func main () {//value is 0, array is empty, empty string, etc. returns TrueFmt. Println ("IsEmpty:", Gocast. IsEmpty (0))//isempty:true//conversion date, need to have time zonet, _: = Gocast. Parsetime ("2012-11-01t22:08:41+00:00") Fmt. Println ("parsetime:", T)//parsetime:2012-11-01 22:08:41 +0000 +0000//reflection Gets the column name of the object and, if there is a tag, takes precedence over its tag Value. var ar accessregion var props= Gocast. Structfields (ar,"Tag")     for_, p: =Range Props {fmt. Print (p,"\ t")//RegionID providerid region_name sub_region_names billing_region_name Description} Fmt. Println ()//the column name of the reflection get object is called key, and the column name is called the map collection of value, and if there is a tag, the value of its corresponding key is the name specified in Tag. var myMap map[string]string= Gocast. Structfieldtags (ar,"Tag")     fork, v: =Range MyMap {fmt. Print ("key:"K"value:"V"\ t")//key:billing_region_name value:billing_region_name key:description value:description key:region_id Value:RegionI D key:provider_id Value:providerid key:region_name value:region_name key:sub_region_names value:sub_region_names< /c1>} Fmt. Println ()//the reflection Gets the column name of the object and the two array with the column name at the time of the tag, and if there is no corresponding tag tag, the column name is Used. var keys, vals = Gocast. Structfieldtagsunsorted (ar,"Tag") Fmt. Print ("Keys:")     for_, p: =Range Keys {fmt. Print (p,"\ t")//keys:region_id provider_id region_name sub_region_names billing_region_name Description} Fmt. Print ("\r\nvals:")     for_, p: =Range Vals {fmt. Print (p,"\ t")//Vals:regionid providerid region_name sub_region_names billing_region_name Description} Fmt. Println ()//convert the parameter 1 of the To method to the type of parameter 2. Tag is used when the type is slice, Map, struct. var obj, _ = Gocast. to (0,true,"") Fmt. Println ("to :"Obj"targetType:", Reflect. TypeOf (obj). Kind ())//To:false Targettype:bool//converts the specified object to bool, where a string type is passed, with only "true" or "1" true and the other False. Numeric type, As long as it is not 0, is True. The collection type is true whenever the collection length is not 0. Fmt. Println (gocast. Tobool (0.00001), Gocast. Tobool ("True")! = Gocast. Tobool ("true"))//true True//will Reflect. The value type is converted to type Bool. Fmt. Println (gocast. Toboolbyreflect (reflect. ValueOf (0.00001)))//true//when converting the specified object to a float64 type, bool value, true to 1,false to 0, the collection type returns 0.Fmt. Println (gocast. Tofloat (true))//return 1//converts the specified object to the int64 type and then to the float32 type. This method will lose the decimal digits. Fmt. Println (gocast. ToFloat32 ("0.001"))//return 0//converts each element in the given slice object to float64 after it is combined into a []float64 object. var arr = Make ([]Interface{},0) Arr= Append (arr,"0.1") Arr= Append (arr,true) Arr= Append (arr,false) Arr=append (arr, nil) arr= Append (arr,3.1415) Fmt. Println (gocast. Tofloat64slice (arr))//[0.1 1 0 0 3.1415]//assigns a pair of pixels to an intFmt. Println (gocast. ToInt ("2"), Gocast. ToInt (int64 (3)))//2 3//converts each element in a given slice object to float64 after it is combined into a []int objectFmt. Println (gocast. Tointslice (arr))//[0 1 0 0 3]//stores the specified slice in the slice of another new interface{} Type. Fmt. Println (gocast. Tointerfaceslice (arr))//[0.1 true false <nil> 3.1415]//converts the type of the first parameter to the type of the specified type. Tag is used when the type is slice, Map, struct. Fmt. Println (gocast. ToT (0, Reflect. TypeOf (true),""))//false <nil>//will specify a pair like a string type, nil will be converted to an empty stringFmt. Println (gocast. ToString (nil), Gocast. ToString (1), Gocast. ToString (ar))//1 {0 0}}

 

third, the test results:

IsEmpty:trueparsetime: -- one- on  a: ,: a+0000+0000RegionID providerid region_name sub_region_names billing_region_name Description Key:billing_region_ Name Value:billing_region_name key:description value:description key:region_id Value:regionid key:provider_id val Ue:providerid key:region_name value:region_name key:sub_region_names value:sub_region_names Keys:region_id Pro vider_id region_name sub_region_names billing_region_name description Vals:regionid ProviderID Region _name sub_region_names billing_region_name Description to:falseTargetType:BOOLtrue truetrue10[0.1 1 0 0 3.1415]2 3[0 1 0 0 3][0.1 true false<nil>3.1415]false<nil>1{0 0}

The use of the Go Language type conversion Library "github.com/demdxx/gocast"

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.