A data type specifies what a value represents and how much memory to allocate for the value. In many cases, you don’t need to specify the data type explicitly such as when declaring variables. It may be inferred based on right-hand side expression. It is also possible to convert between types in Go. For example, a
float64 value may be converted to an
int and vice versa.
In this tutorial, we’ll take a look at the basic types in Go that are the foundation for all other types in the language, including user created types. This investigation into data types is not exhaustive, but it will help you become more familiar with how types work in the language. We’ll also consider basic numeric operations and type conversion in Go.
An integer is a number without a fractional component, and it may be signed or unsigned. A signed integer is one that may be positive or negative, while an unsigned integer is always positive. Go provides four distinct sizes for both types of integers which can be seen in the table below:
In addition to these, Go also provides two other types simply called
uint whose size may be 32 or 64 bits depending on the CPU architecture of the computer the program is running on. That is,
uint will be 32 bits on a 32-bit computer, and 64 bits on a 64-bit one.
int is the most used integer type in the language and is what you should opt for when working with integers except if you have a specific reason to use something else. If you declare an integer variable without explicitly annotating its type, it will be of type
Go does not have a specific type for characters (such as
char in other languages) so it uses the
rune type to represent Unicode character values. The
rune type is an alias for the
int32 type and is equivalent in all ways.
A rune is represented using single quotes, and each one maps to a number (its Unicode codepoint) which is what actually gets stored. For example, the rune literal ‘A’ maps to the number 65. Runes can be used for any type of character in any language, not just ASCII characters. Japanese, Chinese, and Korean characters, accented letters, and even emoji are all valid rune values in Go.
Similar to the
byte is also an alias for an integer type. In this case, it’s
byte type is used to indicate that a value is a piece of raw data rather than a small number and it must be explicitly annotated unlike
In Go, a string is an immutable sequence of bytes. You can represent a string using double quotes, or back ticks (also known as back quotes) as shown below:
String immutability means that the byte sequence that make up the string cannot be changed:
You can of course assign a string to as many variables as you want, and even append to it while doing so. Concatenating a string does not change the original string as is observed below:
The difference between double quoted strings and strings enclosed with back ticks is that the former does not support newlines but can contain escape characters such as
\t for example:
On the other hand, strings enclosed with back ticks are called Raw string literals. They are displayed exactly as they are written and do not support escape characters. They are often use to construct strings that span multiple lines:
Go has two primitive types for floating-point numbers (decimal numbers). They are
float64 which are 32 bits and 64 bits in size respectively. The default type for a floating-point number, if one is not specified, is
float64 type should be preferred in most cases because it has much better precision that
Like most languages, there are two Boolean types in Go:
false. A Boolean type may be annotated using
bool where necessary.
Boolean values are also generated when using comparison operators such as
All the basic numeric operations such as addition, subtraction, multiplication, division, e.t.c. are fully supported in Go and they work on all number types:
We also have increment (
++) and decrement (
—) operators, bitwise operators (
>>), assignment operators (
/=, e.t.c.) and more.
Unlike some other languages, Go does not allow implicit type conversion when performing operations on values with different types:
The solution is to convert between the two number types using the
In this article, we discussed the important data types that you’ll mostly be working with in Go, how to perform some operations and basic type conversions that you need to be aware of. We didn’t cover composite types (arrays, slices, structs) and user defined types here, but we will do so in subsequent articles in this series.
Thanks for reading, and happy coding!