Datatypes in Go
In this tutorial, we will learn about the basic datatypes provided in Go language.
Categorically, there are three different types of basic datatypes based on the kind of values they can allow. They are
- Numeric datatypes – these datatypes allow integer and floating point values.
- Boolean or Logical – this datatype can allow logical values.
- Strings – this datatype can allow a sequence of characters.
Numeric Datatypes
There are different kinds of numeric datatypes, especially types that can store integer values, based on the storage and capability to store signed/unsigned values in it. Also we have two types of datatypes float32 and float64 that can store floating point values.
The following table lists out the different datatypes with the keyword and its description.
Data Type | Description |
---|---|
int8 | 8-bit signed integer |
int16 | 16-bit signed integer |
int32 | 32-bit signed integer |
int64 | 64-bit signed integer |
uint8 | 8-bit unsigned integer |
uint16 | 16-bit unsigned integer |
uint32 | 32-bit unsigned integer |
uint64 | 64-bit unsigned integer |
int | Both int and uint contain same size, either 32 or 64 bit. |
uint | Both int and uint contain same size, either 32 or 64 bit. |
rune | It is a synonym of int32 and also represent Unicode code points. |
byte | It is a synonym of int8. |
uintptr | It is an unsigned integer type. Its width is not defined, but its can hold all the bits of a pointer value. |
float32 | 32-bit IEEE 754 floating-point number |
float64 | 64-bit IEEE 754 floating-point number |
In the following example, we define a variables of different types of numeric datatypes.
var x int = 65
var y float32 = 3.14
var z byte = 87
var p rune = 362
Boolean Datatype
Value of type boolean can store only one bit of data. Its either 1 or 0. 1 represents true and 0 represents false. bool keyword is used to define a variable or constant of type boolean.
true and false are the allowed values for a boolean value.
In the following example, we define a variable x of type boolean, and we assign boolean values to it.
var x bool
x = true
x = false
String Datatype
A string datatype specifies that the variable or constant can store a sequence of characters.
In the following example, we define a variable x of type string, and we assign a string to it.
var x string
x = "Hello World!"
Conclusion
In this Golang Tutorial, we learned different basic datatypes in Go language, and how to declare or define variables with these datatypes.