3. Basic data types

The Go language divides data types into four categories: base types, compound types, reference types, and interface types. This chapter introduces basic types, including numbers, strings, and Booleans. Complex data types — arrays (§4.1) and structures (§4.2) — are used to express more complex data structures by combining simple types. Reference types include Pointers (§2.3.2), slices (§4.2), dictionaries (§4.3), functions (§5), and channels (§8). Although there are many types of data, they are all indirect references to a variable or state in a program. This means that changes to data of any reference type affect all copies of that reference.

3.1. The integer

The numeric types of the Go language include several different sizes of integers, floating point numbers, and complex numbers. Each value type determines the corresponding size range and whether positive and negative symbols are supported. Let’s start with integer types.

The Go language provides both signed and unsigned integer operations. There are four signed integer types int8, int16, int32, and int64 of different sizes, corresponding to the signed integer 8, 16, 32, and 64bit size respectively. It corresponds to the uint8, uint16, uint32, and uint64 unsigned integer types.

There are also two signed and unsigned integers int and uint that generally correspond to the machine word size of a particular CPU platform; Int is the most widely used numeric type. Both types have the same size, 32 or 64bit, but we can’t make any assumptions about that; Because different compilers may produce different sizes even on the same hardware platform.

The Unicode character rune type is the equivalent of int32 and is usually used to represent a Unicode code point. The two names are used interchangeably. Similarly, byte is an equivalent of the Uint8 type, which is typically used to emphasize that a value is a raw piece of data rather than a small integer.

Finally, there is the uintptr, an unsigned integer type that doesn’t specify a specific bit size but is big enough to hold Pointers. The Uintptr type is only needed for low-level programming, especially where the Go language interacts with C language libraries or operating system interfaces. We’ll see a similar example in chapter 13, related to the Unsafe package.

Regardless of their exact size, int, uint, and uintptr are siblings of different types. Where int and int32 are also different types, even though int is 32bit, an explicit conversion is required where int is needed as int32, and vice versa.

The signed integer is expressed as the complement of 2, that is, the highest bit is used to represent the signed bit. The range of the signed number of an n-bit is from −2n−1-2^{n-1}−2n−1 to 2n−1−12^{n-1}-12n−1−1 −1. All bits of an unsigned integer are used to represent non-negative numbers, ranging from 0 to 2n−12^n-12n−1. For example, integers of type Int8 range from -128 to 127, while uint8 integers range from 0 to 255.

Here are the binary operators for arithmetic, logical, and comparison operations in Go, in descending order:

* / % < < > > && ^ + - | ^ = =! = < < = > > = && | |Copy the code