In Go, you can calculate the logarithm of a number using the math
package's Log10
or Log2
functions. Here's an example:
package main
import (
"fmt"
"math"
)
func main() {
// Calculate the logarithm base 10 of a number
num := 100.0
log10 := math.Log10(num)
fmt.Println("Log10:", log10)
// Calculate the logarithm base 2 of a number
log2 := math.Log2(num)
fmt.Println("Log2:", log2)
}
In the example above, we calculate the logarithm base 10 of the num
variable using math.Log10
and store the result in the log10
variable. Similarly, we calculate the logarithm base 2 using math.Log2
and store the result in the log2
variable.
Note that both math.Log10
and math.Log2
return the logarithm of the given number as a float64
value.