Skip to main content
  1. Tutorials/

🏋️‍♂️ Calculate the power of a number x^y in Go

·1 min

To raise a number X to the power of Y in Go, use the Pow(x, y float64) function from the math package.

package main

import (
    "fmt"
    "math"
)

func main() {
    res := math.Pow(2, 7) // 2^7
    fmt.Println(res)
}

Output:

128

To calculate the power of 10 up to the Y exponent in Go, use the math.Pow10(n int) function.

package main

import (
    "fmt"
    "math"
)

func main() {
    res := math.Pow10(3) // 10^3
    fmt.Println(res)
}

Output:

1000