Go int type convert string, by reflection type comparison

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

1. When you spell a string, the following syntax will cause an error:

Mismatched types string and int

Func geterrorjsondata (status int, msg string) string {jsons: = "{\" status\ ":" +status+ ", \" message\ ": \" "+ msg +" \ "}" Retu RN Jsons}
The correct wording is: need to introduce " StrConvThe package
Func geterrorjsondata (status int, msg string) string {jsons: = "{\" status\ ":" + StrConv. Itoa (status) + ", \" message\ ": \" "+ msg +" \ "}" return jsons}
2. Reflection Type Comparison

var user map[string]interface{}
<pre name= "code" class= "HTML" >user["username"] get the parameters in the map

Reflect. TypeOf (user["username"])//Get the type in map
Reflect. TypeOf (user["username"]) ==reflect. string//matches string, success is true, otherwise false



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.