Go Float Data Types

Go Tutorial

Go Float Data Types – Complete Guide with Examples

In Go (Golang), float data types are used to store decimal (fractional) numbers such as prices, percentages, measurements, and scientific values.

Go keeps floating-point handling simple, strict, and predictable.


 What Are Float Data Types in Go?

Float data types store numbers with decimal points.

Example:

  •  Used for real-world numeric values
  • Supports mathematical operations

Float Data Types in Go

Go provides two floating-point types:

TypeSizeDescription
float3232-bitSingle precision
float6464-bitDouble precision (default)

Important:
The default float type in Go is float64.


 Declaring Float Variables

Using var


Using Short Declaration (:=)

  •  Go automatically assigns float64

Default (Zero) Value of Float

If a float variable is declared but not initialized, its default value is 0.0.

Output

0

Float Precision Difference


 

  • float64 stores more accurate values
  •  Preferred for most applications

Type Conversion with Float

Go does not allow automatic type conversion.

 Invalid

 Correct


Mathematical Operations with Float


 

  •  Supports all basic arithmetic operations

Float Comparison

Float values can be compared, but with care.

 Reason: Floating-point precision issues

Recommended Way


 


Formatting Float Output

Basic Output


Precision Control

 Output: 3.14


Smart Formatting

  •  Removes unnecessary zeros

Scientific Notation

Go supports scientific notation using e or E.

Output: 1230


When to Use float32 vs float64

Use CaseRecommended
General programmingfloat64
Scientific calculationsfloat64
Graphics / memory-limitedfloat32
Financial apps Avoid floats (use integers/decimal libs)

 What Go Does NOT Allow

 No f suffix

 No implicit conversion


Common Mistakes

  •  Expecting exact float comparisons
  •  Using float32 unnecessarily
  •  Assuming automatic type conversion
  •  Using floats for money values

 Best Practices for Go Float Types

  •  Use float64 by default
  •  Avoid float equality checks
  •  Use proper formatting verbs
  •  Convert types explicitly
  •  Avoid floats for currency

 Interview Questions: Go Float Data Types

1. Default float type in Go?
float64

2. How many float types does Go have?
Two: float32 and float64

3. Does Go support 3.14f?
No

4. Can Go auto-convert int to float?
No

5. Is float comparison safe?
No, use tolerance checks


 Summary

  •  Go has two float types
  • float64 is default
  •  Zero value is 0.0
  •  Explicit type conversion required
  • Care needed with precision

Mastering Go float data types helps you write accurate, efficient, and reliable numeric programs

You may also like...