In Go, you can use the math.Pow
function from the math
package to calculate the power (exponentiation) of a number.
Here's an example:
package main
import (
"fmt"
"math"
)
func main() {
base := 2.0
exponent := 3.0
result := math.Pow(base, exponent)
fmt.Printf("%.2f^%.2f = %.2f\n", base, exponent, result)
}
The math.Pow
function takes two arguments: the base number and the exponent. It returns the base raised to the power of the exponent.
In the example above, we calculate 2.0 raised to the power of 3.0 and store the result in the result
variable. Finally, we print the formatted result using fmt.Printf
. The output will be 2.00^3.00 = 8.00
.