Golang Type Conversions

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

Golang is a strongly typed language, which is used in the basic type conversion process. 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, ten). Func Itoa (i int) string {return Formatint (Int64 (i), 10)}

Can be seen is a simple implementation of the Formatint method.

String to reshape

I, _: = StrConv. Atoi ("+") fmt. Println (i)

64-bit shaping of the turn string

var i int64i = 0x100fmt. Println (StrConv. Formatint (i, 10))

Formatint The second parameter represents the binary, and 10 represents the decimal.

BYTE-to-32-bit shaping

B: = []byte{0x00, 0x00, 0x03, 0xe8}bytesbuffer: = bytes. Newbuffer (b) var x int32binary. Read (Bytesbuffer, Binary. Bigendian, &x) fmt. PRINTLN (x)

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

32-bit shaping of the turn byte

var x int32x = 106bytesBuffer: = 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.