Golang: underlying data type

Source: Internet
Author: User

Underlying data type

Integral type

Int,uint is based on the CPU platform machine Word size determines the range of values.

&      位运算 AND|      位运算 OR^      位运算 XOR&^     位清空 (AND NOT)<<     左移>>     右移

Default value

    var a1 int             //0    var a2 float64         //0    var a3 bool            //false    var a4 byte            //0    var a5 rune            //0    var a6 []int           //[]    var a7 []byte          //[]    var a8 map[int64]int64 //map[]    var a9 string          //空    var a10 complex64      //(0+0i)

Overflow condition

The result of an arithmetic operation, whether signed or unsigned, is that if more bits are needed to be correctly represented, the result is an overflow. The bit bit portion of the excess high will be discarded. If the original value is a signed type, and the leftmost bit is 1, then the end result may be negative, for example int8

var u uint8 = 255fmt.Println(u, u+1, u*u) // "255 0 1"var i int8 = 127fmt.Println(i, i+1, i*i) // "127 -128 1"

Type conversions

For each type of T, the type conversion operation T (x) converts x to type T if the conversion allows. The conversion of many integers does not change the values; they just tell the compiler how to interpret the value. However, for converting a large integer type to an integer type of small size, or converting a floating-point number to an integer, you may change the value or lose precision:

f := 3.645 // a float64    i := int(f)    fmt.Println(f, i) // "3.141 3"    f = 1.99    fmt.Println(int(f)) // "1"

Techniques for using FMT

Two techniques for using FMT. Usually the printf format string contains more than one% parameter and will contain the same number of extra operands, but the [1] adverb after% tells the printf function to use the first operand again. Second,% of the # adverbs tell printf to generate a 0, 0x, or 0X prefix when outputting with%o,%x, or%x.

ascii := 'a'unicode := '国'newline := '\n'fmt.Printf("%d %[1]c %[1]q\n", ascii)   // "97 a 'a'"fmt.Printf("%d %[1]c %[1]q\n", unicode) // "22269 国 '国'"fmt.Printf("%d %[1]q\n", newline)       // "10 '\n'"

String

A string is a sequence of bytes that cannot be changed.

s := "hello, world"    fmt.Println(len(s))     // "12"    fmt.Println(s[0], s[7]) // "104 119" ('h' and 'w')    fmt.Println(s[:5])      // "hello"    fmt.Println(s[7:])      // "world"    fmt.Println(s[:])       // "hello, world"

Strings can be compared with = = and <, and comparisons are done by byte-by-bit comparisons, so the result of comparison is the order in which strings are naturally encoded.

Because the string is not modifiable , the attempt to modify the internal data of the string is also forbidden:

s[0] = 'L' // compile error: cannot assign to s[0]

Encoding method

Ascii

The earliest period, ASCII is 7bit, can only put 128 characters. Cannot represent the characters of various countries around the world, so there is Unicode

Unicode

With 4byte,32bit, you can represent more characters. But it can create a waste of space, and some characters need only 8bit to be enough. So, with the UTF-8

UTF-8

is a variable-length encoding.
The UTF8 encoding uses 1 to 4 bytes to represent each Unicode code point, the ASCII partial character uses only 1 bytes, and the characters commonly used part is represented by 2 or 3 bytes. The high-end bit bit of the first byte after each symbol encoding is used to indicate how many bytes are encoded in total. If the high-end bit of the first byte is 0, it represents the ASCII character corresponding to 7bit.

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.