Skip to content

Instantly share code, notes, and snippets.

@oldergod
Created September 13, 2014 06:27
Show Gist options
  • Select an option

  • Save oldergod/8b17d29a04e7498b0be5 to your computer and use it in GitHub Desktop.

Select an option

Save oldergod/8b17d29a04e7498b0be5 to your computer and use it in GitHub Desktop.
Tour of Go Errors
package main
import (
"fmt"
"math"
)
type ErrNegativeSqrt float64
func (e ErrNegativeSqrt) Error() string {
return fmt.Sprintf("WHAT THE FUCK IS THAT: %.0f", float64(e))
}
func Sqrt(f float64, z float64) (float64, error) {
if f < 0 {
return math.NaN(), ErrNegativeSqrt(f)
}
for i := 0; i < 10; i++ {
z = z - (z*z-f)/(z*z)
}
return z, nil
}
func main() {
fmt.Println(Sqrt(2, 1))
fmt.Println(Sqrt(-2, 1))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment