Go Data Types

Go Data Types – Complete Guide with Examples
In Go (Golang), data types define what kind of data a variable can store. Go is a statically typed language, which means every variable has a specific type known at compile time.
Understanding Go data types is essential for writing correct, efficient, and bug-free programs.
What Are Data Types in Go?
A data type specifies:
The kind of value a variable can hold
How much memory it uses
What operations can be performed on it
Example:
Categories of Data Types in Go
Go data types are mainly divided into:
- Basic Data Type
- Composite Data Type
- Reference Data Type
1. Basic Data Type
Integer Types
Used to store whole numbers.
| Type | Size |
|---|---|
int | Platform dependent |
int8 | 8-bit |
int16 | 16-bit |
int32 | 32-bit |
int64 | 64-bit |
uint | Unsigned int |
uint8 | Byte |
uint16 | Unsigned 16-bit |
uint32 | Unsigned 32-bit |
uint64 | Unsigned 64-bit |
Example:
Floating-Point Type
Used for decimal numbers.
| Type | Description |
|---|---|
float32 | 32-bit decimal |
float64 | 64-bit decimal (default) |
Boolean Type
Stores true or false.
- Only
trueorfalseallowed
String Type
Stores text data.
- Strings are immutable
- Use double quotes
Rune Type
Represents a single Unicode character.
runeis an alias forint32.
2. Composite Data Types
Array
Fixed-size collection of elements.
- Size is fixed
- Cannot grow
Slice
Dynamic version of arrays.
- Flexible
- Most commonly used
Struct
Used to group different data types.
- Custom data type
- Very powerful
3. Reference Data Types
Pointer
Stores memory address of a variable.
- Efficient
- Used to modify values
Map
Stores key-value pairs.
- Fast lookup
- Reference type
Function Type
Functions are also data types.
- Can be passed as arguments
Special Data Types
interface{} (Empty Interface)
Can hold any type.
- Flexible
- Use carefully
byte
Alias for uint8.
- Used for binary data
Type Inference in Go
Go automatically detects data type.
- Cleaner syntax
Zero Values in Go
If a variable is declared without initialization, Go assigns a zero value.
| Type | Zero Value |
|---|---|
int | 0 |
float64 | 0.0 |
bool | false |
string | "" |
pointer | nil |
Type Conversion (Not Automatic)
Go does not allow implicit type conversion.
Invalid
Valid
Common Mistakes
- Assuming automatic type conversion
- Using wrong data type
- Confusing array and slice
- Ignoring zero values
- Overusing
interface{}
Best Practices
- Use correct data type
- Prefer slices over arrays
- Avoid unnecessary type conversions
- Use structs for related data
- Keep types simple
Interview Questions: Go Data Type
1. Is Go statically typed?
Yes.
2. Default float type in Go?float64.
3. What is a rune?
Alias for int32.
4. Are strings mutable in Go?
No.
5. What is zero value?
Default value of a type.
Summary
- Go has strong, static typing
- Data type define value behavior
- Basic, composite, and reference types
- Zero values avoid uninitialized bugs
- Correct data type improve performance
Mastering Go data type is the foundation of clean, efficient, and professional Go programming
