Share

# Numeric Data Types In Go

Home » Programming Language » Golang » Numeric Data Types In Go

Go has native support for integers and floating-point numbers, as well as complex numbers.

## Integers

Go offers support for four different sizes of signed and unsigned integers named int8, int16, int32, int64; and uint8, uint16, uint32, and uint64, respectively. The number at the end of each type shows the number of bits used for representing each type.

Additionally, int and uint exist and are the most efficient signed and unsigned integers for your current platform. Therefore, when in doubt, use int and uint, but have in mind that the size of these types changes depending on the architecture.

The difference between signed and unsigned integers is the following: if an integer has eight bits and no sign, then its value can be from binary 00000000 (0) to binary 11111111 (255). If it has a sign, then its values can be from -127 to 127. This means that you get to have seven binary digits to store your number because the eight bit is used for keeping the sign of the integer. The same rule applies to other sizes of unsigned integers as well.

## Floating-Point Numbers

Go supports only two types of floating-point numbers: float32 and float64. The first one provides about six decimal digits of the precision, and the second one gives you 15 digits of precision

## Complex Numbers

Similar to floating-point numbers, Go offers two complex number types named complex64 and complex128. The first one uses two float32: one for the real part and the other for the imaginary part of the complex number, whereas complex128 uses two float64. Complex numbers are expressed in the form of a + bi, where a and b are real numbers, and i is a solution of the equation x2 = −1.

All these numeric data types are illustrated below, which will be presented in three parts.

The first part of code is as follows:

```package main

import (
"fmt"
)

func main() {
c := 12 + 1i
c2 := complex(5, 7)
fmt.Printf("Type of c: %T\n", c)
fmt.Printf("Type of c: %T\n", c2)

var c3 complex64 = complex64(c + c2)
fmt.Println("c3:", c3)
fmt.Printf("Type of c: %T\n", c3)

cZ := c3-c3
fmt.Println("cZ:", cZ)```

In this part, we are working with complex numbers and making some calculations with them. There are two ways to create a complex number: directly, as with c and c2, or indirectly by making calculations with existing complex numbers, as c3 and cZ.

Note: If you mistakenly try to create a complex number as aComplex := 12 + 2 * i, there will be two possible outcomes because this statement tells Go that you want to perform an addition and a multiplication. If there is no numeric variable named i in the current scope, this statement create a syntax error and the compilation of your Go code will fail. However, if a numeric variable name i is already defined, the calculation will be successful, but you will not get the desired complex number as the result(bug).

The second portion of the code is the following:

```// integers
x := 10
k := 3

fmt.Println(x)
fmt.Printf("Type of x: %T\n", x)
fmt.Printf("Type of k: %T\n", k)

d := x / k
fmt.Println("Value of d =", d)```

In this part, we are working with signed integers. Please note that if you divide two integers, Go thinks that you want the result of the integers division and will calculate and return quotient of the integer division. So trying to divide 10 by 3 will result in 3 in an integer division and not 3.333333333333333.

Note: When you are converting a floating-point number to an integer, the fraction is discarded by truncating the floating-point number toward zero, which means that some data might get lost in the process.

The last part of the code includes the following code:

```// float
var m, n float64
m = 1.212
fmt.Println("m, n:", m, n)

y := 3 / 2.2
fmt.Println("y:", y)

dFloat := float64(x) / float64(k)
fmt.Println("dFloat:", dFloat)
fmt.Printf("Type of dFloat: %T\n", dFloat)```

In this last of the code, we are working with floating-point numbers. In the presented code, you can see how we can use float64() to tell Go to create a floating-point number when dividing two integers.

If you just update dFloat := float64(x) / k, then you will get the following error message when running the code:

``````\$ go run main.go
# command-line-arguments
./main.go:40:24: invalid operation: float64(x) / k (mismatched types float64 and int)``````

## Full Code

```package main

import (
"fmt"
)

func main() {
// complex numbers
c := 12 + 1i
c2 := complex(5, 7)
fmt.Printf("Type of c: %T\n", c)
fmt.Printf("Type of c: %T\n", c2)

var c3 complex64 = complex64(c + c2)
fmt.Println("c3:", c3)
fmt.Printf("Type of c: %T\n", c3)

cZ := c3 - c3
fmt.Println("cZ:", cZ)

// integers
x := 10
k := 3

fmt.Println(x)
fmt.Printf("Type of x: %T\n", x)
fmt.Printf("Type of k: %T\n", k)

d := x / k
fmt.Println("Value of d =", d)

// float
var m, n float64
m = 1.212
fmt.Println("m, n:", m, n)

y := 3 / 2.2
fmt.Println("y:", y)

dFloat := float64(x) / float64(k)
fmt.Println("dFloat:", dFloat)
fmt.Printf("Type of dFloat: %T\n", dFloat)
}
```

Now executing the all code will generate the following output:

``````\$ go run main.go
Type of c: complex128
Type of c: complex128
c3: (17+8i)
Type of c: complex64
cZ: (0+0i)
10
Type of x: int
Type of k: int
Value of d = 3
m, n: 1.212 0
y: 1.3636363636363635
dFloat: 3.3333333333333335
Type of dFloat: float64``````

## Conclusion

Golang have multiple numeric data type which are integers, float and complex numbers and these types and their multiple bits variables as well in which integers have signed and unsigned integers type variables.

## Highlights

### In Python: How to Use the In Keyword

March 20, 2023 ### Encapsulation in Java (The Easy Way)

March 17, 2023 ### C program to display calendar for a given year

March 14, 2023 ### Top 5 Ways to Become Better at JavaScript

March 10, 2023 ### Journey of HTTP request and response: A Comprehensive Guide

March 1, 2023 ### Differences between sync.Map and map in Go

February 27, 2023 ### Build a To-Do App with Local Storage using Vanilla JavaScript

February 24, 2023 