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"))