Go provides a rich set of data types. In addition to basic integer, floating-point, string, and boolean types, it also includes arrays, slices, functions, maps, structs, channels, and more. Go’s basic data types are largely similar to those in other programming languages.
Basic Data Types
Integers
Integer types can be divided into two main categories. By size, they are: int8, int16, int32, and int64. Their corresponding unsigned integer types are: uint8, uint16, uint32, and uint64.
Among them, uint8 is the familiar byte type, int16 corresponds to the short type in C, and int64 corresponds to the long type in C.
| Type | Description |
|---|---|
| uint8 | Unsigned 8-bit integer (0 to 255) |
| uint16 | Unsigned 16-bit integer (0 to 65535) |
| uint32 | Unsigned 32-bit integer (0 to 4294967295) |
| uint64 | Unsigned 64-bit integer (0 to 18446744073709551615) |
| int8 | Signed 8-bit integer (-128 to 127) |
| int16 | Signed 16-bit integer (-32768 to 32767) |
| int32 | Signed 32-bit integer (-2147483648 to 2147483647) |
| int64 | Signed 64-bit integer (-9223372036854775808 to 9223372036854775807) |
Special Integer Types
| Type | Description |
|---|---|
| uint | Equivalent to uint32 on 32-bit operating systems and uint64 on 64-bit systems |
| int | Equivalent to int32 on 32-bit operating systems and int64 on 64-bit systems |
| uintptr | An unsigned integer type used to store a pointer |
Note: When using the int and uint types, you cannot assume that they are 64-bit or 32-bit integers. You must account for differences between operating system platforms.
Important: The length returned by the built-in len() function for an object may vary according to the byte size of the platform. In practice, the number of elements in a slice or map can be represented using int. When binary transmission or file structure definitions are involved, do not use int or uint, so that the file structure is not affected by differences in byte size across compilation target platforms.
Numeric Literal Syntax
Go 1.13 introduced numeric literal syntax, making it easier for developers to define numbers in binary, octal, or hexadecimal floating-point format. For example:
v := 0b00101101 represents the binary number 101101, which is equivalent to decimal 45. v := 0o377 represents the octal number 377, which is equivalent to decimal 255. v := 0x1p-2 represents hexadecimal 1 divided by 2², which is 0.25. Underscores can also be used to separate digits. For example:
v := 123_456 is equal to 123456.
We can use functions from fmt to display an integer in different numeral systems.
| |
Floating-Point Numbers
Go supports two floating-point types: float32 and float64. Both floating-point formats comply with the IEEE 754 standard. The maximum range of float32 floating-point numbers is approximately 3.4e38 and can be defined using the constant math.MaxFloat32. The maximum range of float64 floating-point numbers is approximately 1.8e308 and can be defined using the constant math.MaxFloat64.
When printing floating-point numbers, you can use the fmt package with the %f verb, as shown below:
| |
Complex Numbers
complex64 and complex128
Complex numbers have real and imaginary parts. The real and imaginary parts of complex64 each occupy 32 bits, while those of complex128 each occupy 64 bits.
| |
Booleans
In Go, boolean data is declared using the bool type. A boolean can have only two values: true and false.
- The default value of a boolean variable is
false.- Go does not allow integers to be explicitly converted to booleans.
- Booleans cannot participate in arithmetic operations or be converted to other types.
Strings
Strings are native data types in Go and can be used in the same way as other native data types such as int, bool, float32, and float64. Strings in Go are internally encoded using UTF-8. A string’s value is the content enclosed in double quotes ("). Non-ASCII characters can be included directly in Go source code. For example:
| |
String Escape Sequences
| Escape Sequence | Meaning |
|---|---|
| \r | Carriage return (returns to the beginning of the line) |
| \n | Line feed (moves directly to the same column on the next line) |
| \t | Tab |
| ' | Single quotation mark |
| " | Double quotation mark |
| \ | Backslash |
For example, suppose we want to print a file path on Windows:
| |
Multiline Strings
Use backticks to define a multiline string:
| |
Common String Operations
| Method | Description |
|---|---|
| len(str) | Get the length |
| + or fmt.Sprintf | Concatenate strings |
| strings.Split | Split |
| strings.contains | Check whether it contains |
| strings.HasPrefix,strings.HasSuffix | Check the prefix/suffix |
| strings.Index(),strings.LastIndex() | Find the position of a substring |
| strings.Join(a[]string, sep string) | Join operation |
The specific usage is as follows:
| |
byte and rune Types
The elements that make up a string are called “characters.” Characters can be obtained by iterating over the string or accessing individual string elements. Character literals are enclosed in single quotation marks (’), for example:
| |
Go has two character types:
- The
uint8type, also known as thebytetype, represents a character in theASCIIcharacter set. - The
runetype, which is exactly equivalent to theint32type, represents a character encoded inutf-8, such as a Chinese or Japanese character.
Go uses the special rune type to handle Unicode, making Unicode-based text processing more convenient. The byte type can also be used for default string processing, providing both performance and extensibility.
| |
Modifying Strings
Strings are immutable in Go. To modify a string, you must first explicitly convert it to []byte or []rune, and then convert it back to a string.
This process involves reallocating memory, so the converted string is no longer the original string.
| |
Type Conversion
Go supports only explicit type conversion, not implicit type conversion. This syntax can be used only when conversion between the two types is supported.
The basic syntax for explicit type conversion is as follows:
| |
Here, T represents the target type. The expression may be a variable, a complex operation, a function return value, and so on.
For example, when calculating the hypotenuse of a right triangle using the Sqrt() function from the math package, the function accepts a float64 argument, while the variables a and b are both of type int. Therefore, a and b must be explicitly converted to float64.
| |
Exercise
- Writer a program to count the number of Chinese princes in the string “hello”
| |