Golang StrConv The use of data type conversions

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

StrConv is a library used by Golang to do data type conversions.


Introduce the two most commonly used methods under StrConv, although it is not easy to explain the language so freely can be str (int), int (string), that is concise.


The article wrote a little messy, welcome to spray! In addition, the article continues to update, please go to the original address to view the update.

http://xiaorui.cc/2016/03/08/golang-strconv%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%E7%9A%84%E7% 94%a8%e6%b3%95/

Python <textarea wrap="soft" class="crayon-plain print-no" data-settings="" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">#xiaorui. Ccatoi (String to int) func Atoi (S string) (i int, err error) Itoa (int to string) func Itoa (i int) string</textarea>
123456789 #xiaorui. CCAtoi (string toint)funcAtoi(s string) (I int, ErrError)Itoa (int tostring)funcItoa(I int) string


Examples of use of Atoi itoa:

Python <textarea wrap="soft" class="crayon-plain print-no" data-settings="" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">I, err: = StrConv. Atoi (" -42") s: = StrConv. Itoa ( -42)</textarea>
123 & nbsp i err : = strconv Span class= "Crayon-sy". atoi ( " -42" s : = strconv itoa ( - 42 )


The following is a complete example:

Python Package <textarea wrap="soft" class="crayon-plain print-no" data-settings="" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">main//xiaorui.cc Import ("StrConv") func main () {i, Err: = StrConv. Atoi ("8888") if err! = Nil {panic (err)} i + = 3 println (i) s: = StrConv. Itoa (333) s + = "3" println (s)}</textarea>
12345678910111213141516171819 PackageMain//Xiaorui.ccImport (    "StrConv")funcMain() {    I, Err := StrConv.Atoi("8888")    if Err != Nil {        Panic(Err)    }    I += 3    println(I)          s := StrConv.Itoa(333)    s += "3"    println(s)}

StrConv is not just a type of conversion between a string and an int, but there are many more ways to do it.

Format xxx turns into string strings

Python <textarea wrap="soft" class="crayon-plain print-no" data-settings="" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">Formatboolfunc Formatbool (b bool) Stringformatfloatfunc formatfloat (f float64, FMT Byte, prec, Bitsize int) stringfor Matintfunc Formatint (i int64, base int) Stringformatuintfunc Formatuint (i uint64, base int) string</textarea>
123456789101112 FormatboolfuncFormatbool(b BOOL) stringformatfloatfuncformatfloat(F float64, FMTbyte, Prec, bitsizeint) stringFormatintfuncFormatint(I Int64, Baseint) stringFormatuintfuncFormatuint(I UInt64, Baseint) string


Examples of Formatint:

Result: = StrConv. Formatint (Int64 (value), 10)

Parse xxx is turned into the appropriate format

Python <textarea wrap="soft" class="crayon-plain print-no" data-settings="" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">convert to bool type. B, err: = StrConv. Parsebool ("true") is converted to float type F, err: = StrConv. Parsefloat ("3.1415", 64) is converted to int type I, err: = StrConv. parseint ("42", 10, 64) turns into UINT type U, err: = StrConv. Parseuint ("A", "Ten,")</textarea>
123456789101112 Converted IntoBOOLType.b, Err := StrConv.Parsebool("true")Converted IntoFloatTypeF, Err := StrConv.parsefloat("3.1415", -)Converted IntointTypeI, Err := StrConv.parseint(" -42", Ten, -)Turn intoUINTTypeu, Err := StrConv.Parseuint(" A", Ten, -)

Golang type Conversion There is nothing to say, not clear directly to see the official 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.