Tutorials References Exercises Menu
Paid Courses

Go Numeric Data Types


Numeric Data Types

Go have the following numeric data types:

  • Integers
  • Floating points
  • Complex numbers

In this chapter you will learn about integers and floating point numbers.


Integer Data Types

An integer data type is declared with the int or uint keyword.

Use an integer data type when you need to store a whole number without decimals, like 35, -50, or 1000.

Integers have two categories:

  • Signed (int)
  • Unsigned (uint)

Signed Integers

Signed integers (int) can store both positive and negative values:

Example

package main
import ("fmt")

func main() {
  var x int = -1000
  fmt.Printf("Type: %T, value: %v", x, x)
}
Try it Yourself »

Unsigned Integers

Unsigned integers (uint) can only store non-negative values:

Example

package main
import ("fmt")

func main() {
  var x uint = 1000
  fmt.Printf("Type: %T, value: %v", x, x)
}
Try it Yourself »

Floating Point Data Types

The floating point data types can store positive and negative numbers with a decimal point.

Floating point data types have two categories:

Data Type Size Description
float32 32 bits Stores numbers with decimal points from -3.4e+38 to 3.4e+38.
float64 64 bits Stores numbers with decimal points from -1.7e+308 to +1.7e+308.

Float 32

Example

This example shows how to declare a float32 variable.

package main
import ("fmt")

func main() {
  var x = 123.78
  fmt.Printf("Type: %T, value: %v", x, x)
}
Try it Yourself »

Float 64

The float64 data type can store a larger set of numbers than float32.

Example

This example shows how to declare a float64 variable.

package main
import ("fmt")

func main() {
  var x float64 = 1.7e+308 // scientific notation
  fmt.Printf("Type: %T, value: %v", x, x)
}
Try it Yourself »

Type of Floating Point to Use

The type of float depends on the value the variable has to store.

Example

package main
import ("fmt")

func main() {
  var x float32= 3.4e+39
  fmt.Printf("Type: %T, value: %v", x, x)
}
Try it Yourself »

 Running the code above gives the following error:

./prog.go:4:5: constant 3.4e+39 overflows float32

This is because the number is out of the range of float32. We should use float64.