Go Float Data Types
Go (Golang) – Floating-Point (Float) Data Types
Floating-point data types in Go are used to store decimal numbers (numbers with fractional parts).
1️⃣ Float Data Types in Go
Go provides two floating-point types:
| Type | Size | Precision | Use Case |
|---|---|---|---|
float32 |
32-bit | ~7 digits | Memory-efficient |
float64 |
64-bit | ~15 digits | High precision (default) |
2️⃣ Default Float Type
If no type is specified, Go uses float64 by default.
✔ Recommended for most applications
3️⃣ Float Operations
4️⃣ Float Formatting (Output)
Use formatting verbs with fmt.Printf().
5️⃣ Type Conversion with Float
Go does not allow implicit conversion.
❌ Invalid:
✅ Valid:
6️⃣ Float Comparison (⚠ Important)
Floating-point numbers may cause precision issues.
❌ Avoid:
✅ Better approach:
(Requires import "math")
7️⃣ Math Functions with Float
8️⃣ Special Float Values
9️⃣ Float vs Integer Division
🔟 Best Practices
✔ Use float64 by default
✔ Avoid direct equality comparison
✔ Use rounding/epsilon for comparisons
✔ Prefer integers for money (paise/cents)
Summary
-
Go supports
float32andfloat64 -
Default float type is
float64 -
No implicit type conversion
-
Precision issues exist (handle carefully)
