boolean, numeric and string. Each data type has a zero value that is automatically assigned when a variable is declared without an initial value.
Boolean
| Keyword | Values |
|---|---|
bool | true or false |
Zero value:
falseNumeric
| Keyword | Size | Values |
|---|---|---|
uint8/byte | 8-bit | 0 to 255 |
uint16 | 16-bit | 0 to 65535 |
uint32 | 32-bit | 0 to 4294967295 |
uint64 | 64-bit | 0 to 18446744073709551615 |
int8 | 8-bit | -128 to 127 |
int16 | 16-bit | -32768 to 32767 |
int32/rune | 32-bit | -2147483648 to 2147483647 |
int64 | 64-bit | -9223372036854775808 to 9223372036854775807 |
float32 | 32-bit | -3.4e+38 to 3.4e+38 |
float64 | 64-bit | -1.7e+308 to +1.7e+308 |
uint | 32 bit / 64 bit | uint32 / uint64 |
int | 32 bit / 64 bit | int32 / int64 |
Zero value:
0String
| Keyword | Values |
|---|---|
string | ”anything surrounded by double quotes” |
Zero value:
" "Understanding Strings in Go
Technically, a string is a read-only slice of bytes:However, there are many characters that cannot be represented by only a single byte:
len() returns the byte count, not the character count.UTF-8 Encoding
In Go, strings are encoded using UTF-8. In other words, a string represents a sequence of characters encoded as UTF-8 bytes. UTF-8 supports a wide range of characters and symbols, and it uses a variable-length encoding scheme. This means a character can be represented by one or more bytes (up to 4), depending on its Unicode code point.| First code point | Last code point | Byte 1 | Byte 2 | Byte 3 | Byte 4 |
|---|---|---|---|---|---|
| 0 | 127 | 0yyyzzzz | |||
| 128 | 2,047 | 110xxxyy | 10yyzzzz | ||
| 2,048 | 65,535 | 1110wwww | 10xxxxyy | 10yyzzzz | |
| 65,536 | 1,114,111 | 11110uvv | 10vvwwww | 10xxxxyy | 10yyzzzz |
Example: Decoding “é”
For example, the character “é” has a Unicode code point of U+00E9 (233 in decimal). Since 233 is in the range 128 to 2047, it is represented in UTF-8 using two bytes:
195 is 11000011 in binary, and 169 is 10101001.
Matching the bytes using the table above:
- First byte:
11000011(110xxxyy) →110+00011 - Second byte:
10101001(10yyzzzz) →10+101001
00011 + 101001 = 0001101001 (binary) = 233 (decimal)