Welcome to The Coding College! In Go (Golang), integers are a fundamental data type, allowing you to perform arithmetic, indexing, and many other essential operations. Whether you’re building algorithms or managing memory, understanding Go’s integer types is crucial.
This guide will explore the integer types available in Go, their ranges, operations, and best practices.
What Are Integer Data Types?
Integers in Go represent whole numbers without any fractional part. They can be:
- Signed (positive, negative, or zero).
- Unsigned (only non-negative values).
Integer Data Types in Go
1. Signed Integers
Signed integers can hold positive and negative values.
Type | Size (bits) | Range |
---|---|---|
int8 | 8 | -128 to 127 |
int16 | 16 | -32,768 to 32,767 |
int32 | 32 | -2,147,483,648 to 2,147,483,647 |
int64 | 64 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
int * | Platform-specific: 32 or 64 bits depending on the system. |
2. Unsigned Integers
Unsigned integers can only hold non-negative values.
Type | Size (bits) | Range |
---|---|---|
uint8 | 8 | 0 to 255 |
uint16 | 16 | 0 to 65,535 |
uint32 | 32 | 0 to 4,294,967,295 |
uint64 | 64 | 0 to 18,446,744,073,709,551,615 |
uint * | Platform-specific: 32 or 64 bits depending on the system. |
Note: The
int
anduint
types depend on the system architecture (32-bit or 64-bit).
3. Special Integer Types
Type | Description |
---|---|
byte | Alias for uint8 . |
rune | Alias for int32 , used to represent Unicode characters. |
uintptr | Integer large enough to hold a pointer. |
Declaring Integer Variables
Using Explicit Type
var age int = 25
var smallNumber int8 = -100
var largeNumber uint64 = 18446744073709551615
Using Implicit Type
age := 25 // Compiler infers type as `int`.
Integer Operations
- Arithmetic Operators:
- Addition (
+
) - Subtraction (
-
) - Multiplication (
*
) - Division (
/
) - Modulus (
%
)
- Addition (
Example:
package main
import "fmt"
func main() {
a, b := 10, 3
fmt.Println("Addition:", a+b)
fmt.Println("Subtraction:", a-b)
fmt.Println("Multiplication:", a*b)
fmt.Println("Division:", a/b)
fmt.Println("Modulus:", a%b)
}
Output:
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3
Modulus: 1
- Comparison Operators:
- Equal to (
==
) - Not equal to (
!=
) - Greater than (
>
) - Less than (
<
) - Greater than or equal to (
>=
) - Less than or equal to (
<=
)
- Equal to (
Type Conversion
Go does not allow implicit type conversion between different integer types. Use explicit conversion.
Example:
package main
import "fmt"
func main() {
var small int8 = 100
var large int = int(small) // Explicit conversion
fmt.Println(large)
}
Example: Working with Integers
Calculating Factorial
package main
import "fmt"
func factorial(n int) int {
result := 1
for i := 2; i <= n; i++ {
result *= i
}
return result
}
func main() {
num := 5
fmt.Printf("Factorial of %d is %d\n", num, factorial(num))
}
Using Unsigned Integers for Counters
package main
import "fmt"
func main() {
var counter uint = 0
for i := 0; i < 5; i++ {
counter++
fmt.Println("Counter:", counter)
}
}
Best Practices for Integer Usage
- Choose the Right Type
- Use
int
for general-purpose numbers unless specific ranges or memory considerations apply.
- Use
- Avoid Overflow and Underflow
- Ensure calculations remain within the type’s range.
- Use Unsigned Integers Carefully
- Only use
uint
if negative numbers are logically impossible.
- Only use
- Be Explicit with Aliases
- Use
byte
andrune
only for their intended purposes (e.g., handling characters or raw data).
- Use
- Handle Division by Zero
- Ensure the divisor is non-zero to avoid runtime panics.
Conclusion
Go’s integer data types offer flexibility and precision for working with numbers. By understanding their ranges and operations, you can write efficient and safe code.