O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Open the console and create the folder chapter03/recipe11.
  2. Navigate to the directory.
  3. Create the log.go file with the following content:
        package main        import (          "fmt"          "math"        )        func main() {          ln := math.Log(math.E)          fmt.Printf("Ln(E) = %.4f\n", ln)          log10 := math.Log10(-100)          fmt.Printf("Log10(10) = %.4f\n", log10)          log2 := math.Log2(2)          fmt.Printf("Log2(2) = %.4f\n", log2)          log_3_6 := Log(3, 6)          fmt.Printf("Log3(6) = %.4f\n", log_3_6)        }        // Log computes the logarithm of        // base > 1 and x greater 0        func Log(base, x float64) float64 {          return math.Log(x) / math.Log(base)        }
  1. Execute the code by running go run log.go in the main Terminal.
  2. You will see the following output:

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required