Overview
Go supports various number data types including integers, floats, and complex numbers. Understanding how each of these types works is essential when performing mathematical operations in Go applications.
Integer Types
Integer types in Go include signed and unsigned integers of varying bit sizes. Below are some examples:
package main
import "fmt"
func main() {
var num1 int = 89 // Default signed integer
var num2 uint = 395 // Unsigned integer
var num3 int8 = -15 // 8-bit signed integer
fmt.Println(num1, num2, num3)
}Float Types
Floats are used to represent decimal numbers. Go offers float32 and float64 types, where float64 provides double precision.
package main
import "fmt"
func main() {
var num1 float32 = 5.789
var num2 float64 = 123.456789
fmt.Println(num1, num2)
}Complex Numbers
Go also supports complex numbers using complex64 and complex128 types.
package main
import "fmt"
func main() {
var num1 complex64 = 2 + 3i
var num2 complex128 = 5 + 12.1i
fmt.Println(num1, num2)
}Basic Mathematical Operations
Performing arithmetic operations such as addition, subtraction, multiplication, and division are fundamental in many applications.
package main
import "fmt"
func main() {
// Basic arithmetic operations
var a = 10
var b = 5
fmt.Println("Addition:", a+b) // Addition
fmt.Println("Subtraction:", a-b) // Subtraction
fmt.Println("Multiplication:", a*b) // Multiplication
fmt.Println("Division:", a/b) // Division
}Working with Math Package
The Go standard library includes a math package for more complex mathematical functions.
Using Basic Math Functions
package main
import (
"fmt"
"math"
)
func main() {
// Using basic math functions
fmt.Println("Square root:", math.Sqrt(16)) // Square root
fmt.Println("Exponential:", math.Exp(2)) // Exponential function
fmt.Println("Power:", math.Pow(3, 4)) // Power function, 3^4
}Trigonometric Functions
Trigonometric calculations can be performed using built-in functions, which is particularly useful in graphics or physics simulations.
package main
import (
"fmt"
"math"
)
func main() {
var radians = math.Pi / 2 // 90 degrees
fmt.Println("Sine:", math.Sin(radians)) // Output: 1
fmt.Println("Cosine:", math.Cos(radians)) // Output: 0
fmt.Println("Tangent:", math.Tan(radians)) // Output: NA due infinity
}Advanced use of Numbers in Go
Beyond simple calculations, understanding how to use numbers efficiently can affect the performance of your Go application. Here are some advanced topics.
Efficient Use of Integers and Bits
Bit manipulation and optimized data types can help in developing efficient applications, especially those involved in hardware interaction or low-level programming.
package main
import "fmt"
func main() {
var x uint8 = 15
var y uint8 = 2
// Bitwise OR
fmt.Printf("x | y : %08b\n", x|y)
// Bitwise AND
fmt.Printf("x & y : %08b\n", x&y)
// Bitwise XOR
fmt.Printf("x ^ y : %08b\n", x^y)
// Left shift
fmt.Printf("x << 1 : %08b\n", x<<1)
// Right shift
fmt.Printf("x >> 1 : %08b\n", x>>1)
}Concurrency with Number Calculations
Go routines can be used to perform concurrent calculations, demonstrating Go's concurrency model.
package main
import (
"fmt"
"sync"
)
var wg sync.WaitGroup
func calculateSquare(n int) {
defer wg.Done()
fmt.Printf("Square of %d is %d\n", n, n*n)
}
func main() {
numbers := []int{2, 4, 6, 8, 10}
for _, n := range numbers {
wg.Add(1)
go calculateSquare(n)
}
wg.Wait()
}