Implementation of the exercise: Loops and Functions in “A Tour of Go”

Golang mascot by Kirael-Art on DeviantArt

A Tour of Go is an interactive introduction to Go covering basic syntax and data structures, methods and interfaces, and Go’s concurrency primitives. It contains a few exercises to help you practice what you’ve learned.

When you begin to take the tour, you will encounter the Loops and Functions exercise which asks you to implement a square root function using Newton’s method. First, you need to repeat the calculation 10 times and print each value along the way. Here is my implementation of the same.

package main

Then, you are asked to change the loop condition to stop once the value has stopped changing (or only changes by a very small amount). Also, you need to find the closeness of your function’s results to the math.Sqrt in the standard library. Here is how I implemented it.

package main

Developer | Open Source Contributor | Quizzer | Loves to convert Ideas into Code | Github profile: https://github.com/agarwalrounak