Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Go Programming - Math Package Tutorial

Introduction

The math package in Go provides basic constants and mathematical functions. It is an essential part of the Go standard library, widely used for performing mathematical operations.

Importing the Math Package

To use the math package, you need to import it in your Go program:

import "math"

Constants

The math package defines several useful constants, such as Pi, E, and Phi. Here are some examples:

package main

import (
    "fmt"
    "math"
)

func main() {
    fmt.Println("Pi:", math.Pi)
    fmt.Println("E:", math.E)
    fmt.Println("Phi:", math.Phi)
}
                
Pi: 3.141592653589793
E: 2.718281828459045
Phi: 1.618033988749895

Basic Mathematical Functions

The math package provides various functions for basic mathematical operations, such as Sqrt, Pow, and Abs. Let's look at some examples:

package main

import (
    "fmt"
    "math"
)

func main() {
    fmt.Println("Square root of 16:", math.Sqrt(16))
    fmt.Println("2 raised to the power 3:", math.Pow(2, 3))
    fmt.Println("Absolute value of -5:", math.Abs(-5))
}
                
Square root of 16: 4
2 raised to the power 3: 8
Absolute value of -5: 5

Trigonometric Functions

Trigonometric functions are also available in the math package. These include Sin, Cos, and Tan. Here's how you can use them:

package main

import (
    "fmt"
    "math"
)

func main() {
    fmt.Println("Sin(π/2):", math.Sin(math.Pi/2))
    fmt.Println("Cos(π):", math.Cos(math.Pi))
    fmt.Println("Tan(π/4):", math.Tan(math.Pi/4))
}
                
Sin(π/2): 1
Cos(π): -1
Tan(π/4): 1

Logarithmic Functions

The package also includes logarithmic functions like Log, Log10, and Exp. Below are some examples:

package main

import (
    "fmt"
    "math"
)

func main() {
    fmt.Println("Log(10):", math.Log(10))
    fmt.Println("Log10(100):", math.Log10(100))
    fmt.Println("Exp(2):", math.Exp(2))
}
                
Log(10): 2.302585092994046
Log10(100): 2
Exp(2): 7.38905609893065

Rounding Functions

The math package provides functions for rounding numbers, such as Floor, Ceil, and Round. Here are some examples:

package main

import (
    "fmt"
    "math"
)

func main() {
    fmt.Println("Floor(1.8):", math.Floor(1.8))
    fmt.Println("Ceil(1.2):", math.Ceil(1.2))
    fmt.Println("Round(1.5):", math.Round(1.5))
}
                
Floor(1.8): 1
Ceil(1.2): 2
Round(1.5): 2

Other Useful Functions

There are many other useful functions in the math package, such as Min, Max, and Mod. Below are some examples:

package main

import (
    "fmt"
    "math"
)

func main() {
    fmt.Println("Min(3, 5):", math.Min(3, 5))
    fmt.Println("Max(3, 5):", math.Max(3, 5))
    fmt.Println("Mod(7, 3):", math.Mod(7, 3))
}
                
Min(3, 5): 3
Max(3, 5): 5
Mod(7, 3): 1

Conclusion

The math package in Go is a powerful and essential tool for performing a wide range of mathematical operations. This tutorial covered the basics, but there are many more functions available in the package. Refer to the official documentation for more details and advanced usage.