1. While working through the official Go tutorial A Tour of Go, I encountered a classic exercise:
Implement a square root function Sqrt(x). As shown in Figure 1.

This exercise covers not only loops and functions in Go, but also introduces a very important numerical computation method:
Newton’s method.
This article will demonstrate, through three progressively optimized examples, how to start from a simple version
and gradually implement a more accurate and robust square root function:
- Fixed 10 iterations
- Stopping using error control
- Comparing results with the standard library math.Sqrt
All code is organized in the following directory structure:
go-tour/
├── sqrt-iterations/
├── sqrt-epsilon/
├── sqrt-iteration-count/
2. ## Introduction to Newton’s Method
The goal of a square root function is:
Given a number x, find a number z such that:
z² ≈ x
Newton’s method continuously adjusts z to gradually approach the true value.
The update formula for each step is as follows:
z = z – (zz – x) / (2z)
Where:
- z*z – x represents the current error
- 2*z represents the derivative of the function z²
- Each update brings the result closer to the true value
This method converges very quickly,
typically requiring only a few iterations to achieve high precision.
3. Example 1: Fixed 10 iterations, corresponding to sqrt-iterations/main.go
package main
import (
"fmt"
)
func Sqrt(x float64) float64 {
z := 1.0 // 初始猜测值
for i := 0; i < 10; i++ {
z -= (z*z - x) / (2 * z)
fmt.Println("iteration", i, "z =", z)
}
return z
}
func main() {
fmt.Println("result:", Sqrt(2))
}
4. Example output
It can be observed that:
After each iteration, the value of z quickly approaches √2.
Usually, after the 4th to 5th iteration,
the result is already very close to the true value.
package main
import (
"fmt"
)
func Sqrt(x float64) float64 {
z := 1.0 // 初始猜测值
for i := 0; i < 10; i++ {
z -= (z*z - x) / (2 * z)
fmt.Println("iteration", i, "z =", z)
}
return z
}
func main() {
fmt.Println("result:", Sqrt(2))
}
5. Example 2: Using epsilon to control convergence + comparing with math.Sqrt, corresponding to: sqrt-epsilon/main.go
package main
import (
"fmt"
"math"
)
func Sqrt(x float64) float64 {
z := 1.0 // 初始猜测值
const epsilon = 1e-10 // 精度控制
for {
prev := z
z -= (z*z - x) / (2 * z)
// 判断变化是否足够小
if math.Abs(z-prev) < epsilon {
break
}
}
return z
}
func main() {
fmt.Println("My sqrt:", Sqrt(2))
fmt.Println("Math sqrt:", math.Sqrt(2))
}6. Example output
Why is this better?
This approach:
Automatically determines convergence
Avoids meaningless computations
Improves program efficiency
To verify whether our implementation is correct,
we can compare the result with math.Sqrt from the Go standard library.
As we can see, the two are almost completely identical (exactly equal).
/app/go-tour/sqrt-epsilon # go run main.go
My sqrt: 1.4142135623730951
Math sqrt: 1.4142135623730951
7. Example 3: Counting iterations (observing convergence efficiency), corresponding to: sqrt-iteration-count/main.go
package main
import (
"fmt"
"math"
)
func Sqrt(x float64) float64 {
z := 1.0 // 初始猜测值
const epsilon = 1e-10 // 精度控制
count := 0
for {
prev := z
z -= (z*z - x) / (2 * z)
count++
// 判断变化是否足够小
if math.Abs(z-prev) < epsilon {
break
}
}
fmt.Println("iterations:", count)
return z
}
func main() {
fmt.Println("result:", Sqrt(2))
}
8. Example output
Features:
Counts count
Outputs iterations
Usage:
Observe algorithm efficiency.
Typically:
iterations: 5
It only takes 5 iterations to get a high-precision result.
/app/go-tour/sqrt-iteration-count # go run main.go
iterations: 5
result: 1.4142135623730951
