Sling Academy
Home/Golang/Understanding Numeric Data Types in Go: int, float, and more

Understanding Numeric Data Types in Go: int, float, and more

Last updated: November 24, 2024

Go, also known as Golang, is a statically typed programming language that offers various numeric data types to handle numerical operations and computations. This article will dive into the key numeric types in Go, including integers (int), floating points (float), and additional numeric types, with easy-to-understand examples.

Integer Types

Integers are whole numbers without any fractional parts. In Go, there are several built-in integer types which include signed integers (int, int8, int16, int32, int64) and unsigned integers (uint, uint8, uint16, uint32, uint64).

Basic Example: Declaring Integers

package main

import "fmt"

func main() {
    var x int = 10
    fmt.Println(x) // Output: 10
}

Intermediate Example: Using Different Integer Types

package main

import "fmt"

func main() {
    var a int16 = 3456
    var b uint8 = 255
    fmt.Println(a) // Output: 3456
    fmt.Println(b) // Output: 255
}

Advanced Example: Type Conversion

In Go, you need explicit conversion when assigning values of one type to another. Here's how you can do it:

package main

import "fmt"

func main() {
    var a int = 65
    var b float64 = float64(a) // Explicit conversion from int to float64
    fmt.Println(b)            // Output: 65
}

Floating Point Types

Floating-point numbers are used to represent numbers with decimal points. The two main floating-point types in Go are float32 and float64.

Basic Example: Declaring Floats

package main

import "fmt"

func main() {
    var pi float32 = 3.14
    fmt.Println(pi) // Output: 3.14
}

Intermediate Example: Operations with Floats

package main

import "fmt"

func main() {
    var a float64 = 1.23
    var b float64 = 4.56
    var sum float64 = a + b
    fmt.Println(sum) // Output: 5.79
}

Advanced Example: Precision Considerations

Floating-point precision can lead to rounding errors:

package main

import "fmt"

func main() {
    var f float64 = 0.1
    var sum float64 = f + f + f
    fmt.Println(sum) // Output: 0.30000000000000004
}

Other Numeric Types

Go also supports additional numeric types such as complex64 and complex128 for complex numbers.

Basic Example: Complex Numbers

package main

import "fmt"

func main() {
    var c complex64 = complex(5, 7)
    fmt.Println(c) // Output: (5+7i)
}

Advanced Example: Operations with Complex Numbers

package main

import "fmt"

func main() {
    var x complex128 = complex(2, 3)
    var y complex128 = complex(1, 2)
    var result complex128 = x + y
    fmt.Println(result) // Output: (3+5i)
}

Understanding numeric data types in Go is crucial for writing efficient and effective code. By leveraging the various integer and floating-point types, you can handle a wide array of computations in your Go programs.

Next Article: Declaring and Initializing Numeric Variables in Go

Previous Article: Working with Numbers in Go: An Introductory Guide

Series: Numbers and Math in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant