Data types help a programming language understand the type of data it works with. Golang has different built-in data types to handle different types of information.
When you use appropriate data types for your variables or functions, the compiler will be able to optimize your application properly. This will also increase performance.
In this guide, we will take you on a journey through the various data types in Golang, including numeric types, strings, boolean, aggregates like arrays and maps, and more.
You'll have a deep understanding of Golang's type system and in which situation you should use which data type to create powerful and flexible applications.
Different Types of Data in Golang
Golang provides multiple data types for different types of data. Some data types also have multiple variants. Each variant can hold different sizes of data and therefore also take various amounts of memory space.
Golang data types can be divided into these groups:
- Numeric Data Types – Include integers (real numbers without a fractional component), floating-point numbers, and complex numbers.
- String Data Type – Represents a sequence of characters. It is used to store and manipulate textual data.
- Boolean Data Type – Represents logical values and can have two possible values: true or false.
Let's learn more about these groups and their data types in detail with examples.
Also Read: Golang Variable - Different Ways to Create Variables in Golang
Numeric Data Types
In Golang, we use numeric data types for numbers. When you declare a variable and set its type as an int
, this means you can only store numbers in that variable.
Golang offers a rich set of numeric types to fulfill different needs. We can divide numeric data types into 3 groups: integer, float, and complex types.
Integer Type
Integers are whole numbers without a fractional component. Golang provides both signed and unsigned integer types, with varying sizes and ranges.
Signed integers can represent both positive and negative numbers. On the other hand, unsigned integers can only represent non-negative numbers (zero and positive values).
For an integer type, you can use the following keywords:
- Signed integer:
int8
,int16
,int32
,int64
- Unsigned integer:
uint8
,uint16
,uint32
,uint64
As you can see, there are many keywords to represent integer types. To use them properly, it is important to know their differences.
Signed Integer Types:
int8
– Signed 8 bits integer. It can hold numbers from -128 to 127.int16
– Signed 16 bits integer. It can hold numbers from -32,768 to 32,767.int32
– Signed 32 bits integer. It can hold numbers from -2,147,483,648 to 2,147,483,647.int64
– Signed 64 bits integer. It can hold numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
Unsigned Integer Types:
uint8
– Unsigned 8 bits integer. It can hold numbers from 0 to 255.uint16
– Unsigned 16 bits integer. It can hold numbers from 0 to 65,535.uint32
– Unsigned 32 bits integer. It can hold numbers from 0 to 4,294,967,295.uint64
– Unsigned 64 bits integer. It can hold numbers from 0 to 18,446,744,073,709,551,615.
All of them can hold whole numbers but they are different in range. More bits can store bigger numbers and also takes up more memory space.
// Signed integers
var a int32 = 1439
var b int64 = -492874
// Unsigned integers
var x uint32 = 4293
var y uint64 = 4294967
Therefore, you can use these specific integer types if you want to optimize the memory consumption of your application. Otherwise, simply use int
or uint
as the integer type.
These int
and uint
types are machine dependent. If you are using a 32-bit CPU in your computer, they will represent int32
and uint32
. But if you have a 64-bit CPU in your computer, they will behave like int64
and uint64
.
// Default type of this "age" variable will be int
var age = 26
If you initialize a variable with a number without setting the type, it will be int
by default.
Float Type
Floating-point numbers represent real numbers with a fractional component. It represents both positive and negative numbers.
Golang provides two float types: float32
and float64
The choice between these types depends on the required precision and range of values. The float32
is a single-precision floating-point number, while float64
is a double-precision floating-point number.
Type | Size | Range | Precision |
---|---|---|---|
float32 | 32 bits (4 bytes) | Approximately ±1.18e-38 to ±3.4e38 | Typically around 7 decimal digits |
float64 | 64 bits (8 bytes) | Approximately ±2.23e-308 to ±1.80e308 | Typically around 15 decimal digits |
var a float32 = -53.45
var b float64 = 540432.49342
The choice between float32 and float64 depends on the specific needs of your application. Here are some considerations:
- Precision:
float64
provides a higher precision thanfloat32
due to its larger size. If your calculations require more accurate results with a higher number of decimal places,float64
is the recommended choice. - Range:
float64
also has a wider range, allowing you to represent larger or smaller values without losing precision. If you need to handle extremely large or small numbers,float64
is more suitable. - Memory:
float32
consumes less memory thanfloat64
since it has a smaller size. If memory usage is a concern, or if you're working with a large number of floating-point values,float32
can be more efficient.
var price = 37.99
When you don't set the type of a floating-point number, Golang will consider it as a float64
type by default.
Complex Type
In Golang, the complex type is used to represent complex numbers, which consist of a real part and an imaginary part. The complex type in Golang includes two sizes: complex64
and complex128
The complex64
is a single-precision number and complex128
is a double-precision number. If you don't specify the type of a complex variable, it will be complex128
by default.
To create a complex number in Golang, you can use the complex
function or directly assign values to the complex variable. We separate the real part and imaginary part are separated by the +
or -
sign, followed by the i
(imaginary unit).
package main
import "fmt"
func main() {
// complex number with real part 2 and imaginary part 3
var z1 complex128 = complex(2, 3)
// shorthand declaration of a complex number
z2 := 1 + 2i
// Performing arithmetic operations on complex numbers
sum := z1 + z2
difference := z1 - z2
product := z1 * z2
quotient := z1 / z2
// Printing the results
fmt.Println("Sum:", sum)
fmt.Println("Difference:", difference)
fmt.Println("Multiplication:", product)
fmt.Println("Division:", quotient)
}
Output:
Sum: (3+5i)
Difference: (1+1i)
Multiplication: (-4+7i)
Division: (1.6-0.2i)
In the example, we create two complex numbers, z1
and z2
, using different syntaxes. We then perform various arithmetic operations like addition, subtraction, multiplication, and division on these complex numbers.
Complex numbers are useful in mathematical and scientific computations that involve real and imaginary components.
String Data Type
We use the string
data type is used to represent a sequence of characters. You need to use double quotes ("") or backticks (``) to create strings. For example, "Hello World"
var title string
title = "Learn Golang"
Boolean Data Type
The boolean data type is a fundamental type that represents logical values. We use the boolean values in conditional statements, logical operations, and decision-making within Golang programs.
It can have two possible values: true
or false
. To declare boolean data types, you have to use the bool
keyword in Golang.
package main
import "fmt"
func main() {
// Declaring boolean variables
var isPresent bool
isPresent = true
// Conditional statements
if isPresent {
fmt.Println("It is present!")
} else {
fmt.Println("It isn't present!")
}
}
Also Read: Mastering Constant in Golang: Declare and Use It Like a Pro
Conclusion
We have discussed all the primitive data types available in Golang. Understanding the different data types in Golang is crucial for effective programming and data manipulation.
If we recap this lesson, these are the built-in data types you can use:
- Integer – There are 2 types of integer data types to represent numbers: Signed integers (
int8
,int16
,int32
,int64
) and Unsigned integers (uint8
,uint16
,uint32
,uint64
). - Float – Float data type is used to represent floating point numbers. There are 2 variants of float type:
float32
andfloat64
. - Complex – The complex type is used to represent complex numbers with real and imaginary parts. There are 2 variants of complex type:
complex64
andcomplex128
. - Sting – We use the string type to represent a sequence of characters. There is one keyword that is
string
to create a string type. - Boolean – We use the boolean data type to hold
true
andfalse
values. The bool keyword is used to create a boolean type in Golang.
Other than these default data types, you can also create your custom data type in Golang using struct
(structure). You have many flexibilities while working with different types.