Go language type Conversion

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

excerpt from Happy programming »golang Type Conversions

Golang is a strongly typed language, which is used in basic types during application. Here's a look at the common type conversions, which are constantly updated.

Shaping A string

Fmt. Println (StrConv. Itoa (100))

The source code for this method is:

Itoa is shorthand for formatint (I, 10).
Func Itoa (i int) string {
Return Formatint (Int64 (i), 10)
}

can be seen to be Formatint simple implementation of the method.

String to reshape

I, _: = StrConv. Atoi ("100")
Fmt. Println (i)

- bit shaping to string

var i int64
i = 0x100
Fmt. Println (StrConv. Formatint (i, 10))

Formatint The second parameter represents a binary, Ten represents a decimal.

byte Turn + bit Shaping

B: = []byte{0x00, 0x00, 0x03, 0xe8}
Bytesbuffer: = bytes. Newbuffer (b)

var x int32
Binary. Read (Bytesbuffer, Binary. Bigendian, &x)
Fmt. PRINTLN (x)

which binary. The Bigendian represents the byte order, and the corresponding little endian. Popular parlance is called big-endian, small end.

+ bit Shaping bytes

var x int32
x = 106
Bytesbuffer: = bytes. Newbuffer ([]byte{})
Binary. Write (Bytesbuffer, Binary. Bigendian, X)
Fmt. Println (Bytesbuffer.bytes ())

byte-to-string

Fmt. Println (String ([]byte{97, 98, 99, 100})

String to Byte

Fmt. Println ([]byte ("ABCD"))

 

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.