Go Data Types
Understanding data types is fundamental to writing efficient and reliable programs in Go. A data type defines the kind of value a variable can store, as well as how much memory it occupies.
What Are Data Types?
A data type determines:
- The type of data (e.g., number, text, true/false)
- The memory size allocated
- The operations that can be performed on the value
Go is a statically typed language, meaning once a variable is declared with a specific type, it cannot store values of a different type later.
Basic Data Types in Go
Go provides three primary categories of basic data types:
1. Boolean (bool)
Represents logical values:
truefalse
2. Numeric Types
Used for numbers and include:
-
Integers →
int,int8,int16,int32,int64 -
Unsigned integers →
uint,uint8,uint16, etc. -
Floating-point numbers →
float32,float64 -
Complex numbers →
complex64,complex128
3. String (string)
Represents a sequence of characters (text), enclosed in double quotes.
Example: Using Different Data Types
Type Inference in Go
Go can automatically determine the data type of a variable based on the assigned value. This is known as type inference.
Why Data Types Matter
Using the correct data type helps:
- Improve performance and memory efficiency
- Prevent type-related errors
- Make code more readable and maintainable
- Enable better compile-time checks
Boolean Data Type in Go
In Go, the boolean data type is used to represent logical values. A boolean variable can hold only one of two possible values:
truefalse
Booleans are commonly used in conditional statements, loops, and comparisons to control the flow of a program.
Declaring Boolean Variables
A boolean variable is declared using the bool type. Go also
supports type inference, allowing you to omit the explicit type in many
cases.
Default Value of Boolean
If a boolean variable is declared without an initial value, Go automatically
assigns it a default value: false. This ensures predictable
behavior and avoids uninitialized variables.
Using Booleans in Expressions
Booleans are often the result of comparisons or logical operations.
Go Integer Data Types
In Go, integer data types are used to store whole numbers—values without decimal points—such as 42, -100, or 900000. Go provides a rich set of integer types to help you optimize memory usage and handle different numeric ranges efficiently.
Types of Integers in Go
Integer types in Go are divided into two categories:
-
1. Signed Integers: Can store both positive and negative
values. Declared using the
intfamily of types. -
2. Unsigned Integers: Can store only non-negative values
(zero and positive numbers). Declared using the
uintfamily of types.
Tip: If you don’t explicitly specify a type, Go assigns the default
integer type int.
Signed Integer Types
Signed integers allow both positive and negative numbers.
Signed Integer Sizes and Ranges
| Type | Size | Range |
|---|---|---|
int
|
Platform-dependent | Varies (32-bit or 64-bit) |
int8
|
8 bits | -128 to 127 |
int16
|
16 bits | -32,768 to 32,767 |
int32
|
32 bits | -2,147,483,648 to 2,147,483,647 |
int64
|
64 bits | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
Unsigned Integer Types
Unsigned integers store only non-negative values, making them ideal for quantities that cannot be negative (e.g., counts, sizes).
Unsigned Integer Sizes and Ranges
| Type | Size | Range |
|---|---|---|
uint
|
Platform-dependent | Varies (32-bit or 64-bit) |
uint8
|
8 bits | 0 to 255 |
uint16
|
16 bits | 0 to 65,535 |
uint32
|
32 bits | 0 to 4,294,967,295 |
uint64
|
64 bits | 0 to 18,446,744,073,709,551,615 |
Choosing the Right Integer Type
Selecting the appropriate integer type is important for performance and memory efficiency:
-
Use
intfor general-purpose operations -
Use smaller types (
int8,int16) when memory optimization matters - Use unsigned types when negative values are not needed
-
Use
int64oruint64for very large numbers
Go Float Data Types
In Go, floating-point data types are used to store numbers that contain decimal points or require high precision—such as 3.14, -0.75, or 1.2e6. These types are essential for scientific calculations, financial data, and any scenario where fractional values are involved.
Types of Float Data in Go
Go provides two floating-point types:
| Type | Size | Approximate Range |
|---|---|---|
float32
|
32-bit | ±3.4 × 10³⁸ |
float64
|
64-bit | ±1.7 × 10³⁰⁸ |
Tip: The default floating-point type in Go is float64. If
you don’t explicitly specify a type, Go will automatically assign
float64.
Using float32
The float32 type uses less memory but provides lower precision
compared to float64.
When to use float32:
- When memory optimization is important
- When high precision is not critical
Using float64
The float64 type offers higher precision and a much larger
range, making it the preferred choice in most applications.
Scientific Notation in Go
Go supports scientific notation using e or E:
Choosing Between float32 and float64
-
Use
float64for most applications (default choice) -
Use
float32when memory usage is a concern (e.g., large datasets, graphics) - Prefer higher precision when dealing with financial or scientific calculations
Go String Data Type
In Go, the string data type is used to store text—a sequence of characters
such as words, sentences, or symbols. Strings are defined using double
quotes (" ") and are widely used for handling user input,
messages, file data, and more.
Declaring Strings in Go
Go provides multiple ways to declare and initialize string variables, including explicit typing and type inference.
Default Value of String
If a string variable is declared but not initialized, Go assigns it a
default value: "". This represents an empty string, not nil.
String Characteristics
- Strings are immutable (cannot be changed after creation)
- Stored as a sequence of bytes (UTF-8 encoded)
- Can include letters, numbers, symbols, and spaces
Working with Strings
Concatenation Example
Multi-line Strings (Raw Strings)
Go supports raw string literals using backticks (`), which
preserve formatting.
