# How to Calculate Power in Golang

To calculate the power of a number in Go, you can use the “math.Pow()” function from the math package. The math.Pow() function takes two float64 arguments, the base (x) and the exponent (y), and returns the result as a float64 value.

### Example 1

``````package main

import (
"fmt"
"math"
)

func main() {
base := 2.0
exponent := 3.0

result := math.Pow(base, exponent)
fmt.Printf("%v raised to the power of %v is %v\n", base, exponent, result)
}``````

Output

``2 raised to the power of 3 is 8``

In this code example, we imported the math package and use the math.Pow() function to calculate 2 raised to the power of 3 (2³). The result is printed using fmt.Printf() function.

### Example 2

The math.Pow() function works with float64 values. If you need to work with integers, you can write a custom function to calculate the power using integers.

``````package main

import "fmt"

func main() {
base := 2
exponent := 3

result := intPow(base, exponent)

fmt.Printf("%d raised to the power of %d is %d\n", base, exponent, result)
}

func intPow(base, exponent int) int {
result := 1

for i := 0; i < exponent; i++ {
result *= base
}

return result
}``````

Output

``2 raised to the power of 3 is 8``

In this example, we defined a custom intPow() function that takes two integers, base and exponent, and returns an integer result. The function calculates the power using a loop that multiplies the result variable by the base a number of times equal to the exponent.