Copy your Sqrt function from the earlier exercises and modify it to return an error value.

Sqrt should return a non-nil error value when given a negative number, as it doesn't support complex numbers.

Create a new type

type ErrNegativeSqrt float64

and make it an error by giving it a

func (e ErrNegativeSqrt) Error() string

method such that ErrNegativeSqrt(-2).Error() returns "cannot Sqrt negative number: -2".

Note: a call to fmt.Print(e) inside the Error method will send the program into an infinite loop. You can avoid this by converting e first:fmt.Print(float64(e)). Why?

Change your Sqrt function to return an ErrNegativeSqrt value when given a negative number.

package main

import (
"fmt"
"strconv"
)
type ErrNegativeSqrt float64
func (e ErrNegativeSqrt) Error() string{
if e < {
return "cannot Sqrt negative number:" + strconv.FormatFloat(float64(e),'f',,)
}
return ""
}
func Sqrt(f float64) (float64, error) {
var e error
if f < {
return ,ErrNegativeSqrt(f)
}
var z float64 =
for i := ; i < ; i++ {
z = z - (z*z - f) / ( * z)
}
return z,e
} func main() {
fmt.Println(Sqrt())
fmt.Println(Sqrt(-))
}

A Tour of Go Exercise: Errors的更多相关文章

  1. A Tour of Go Exercise: Images

    Remember the picture generator you wrote earlier? Let's write another one, but this time it will ret ...

  2. A Tour of Go Exercise: HTTP Handlers

    Implement the following types and define ServeHTTP methods on them. Register them to handle specific ...

  3. A Tour of Go Exercise: Fibonacci closure

    Let's have some fun with functions. Implement a fibonacci function that returns a function (a closur ...

  4. A Tour of Go Exercise: Maps

    Implement WordCount. It should return a map of the counts of each “word” in the string s. The wc.Tes ...

  5. A Tour of Go Exercise: Slices

    Implement Pic. It should return a slice of length dy, each element of which is a slice of dx 8-bit u ...

  6. A Tour of Go Exercise: Loops and Functions

    As a simple way to play with functions and loops, implement the square root function using Newton's ...

  7. exercise.tour.go google的go官方教程答案

    /* Exercise: Loops and Functions #43 */ package main import ( "fmt" "math" ) fun ...

  8. Go for Pythonistas Go and the Zen of Python 禅

    Go for Pythonistas https://talks.golang.org/2013/go4python.slide#1 Things I don't like about Python ...

  9. A Tour of Go Errors

    An error is anything that can describe itself as an error string. The idea is captured by the predef ...

随机推荐

  1. hdu 2516 取石子游戏 博弈论

    很显然的nim游戏的变形,很好找规律 先手败:2,3,5,8,13…… 其他先手胜.即满足菲波拉数列. 代码如下: #include<iostream> #include<stdio ...

  2. 【Mysql进阶技巧(1)】 MySQL的多表关联与自连接

    自连接 测试数据准备 CREATE TABLE `t2` ( `id` int(11) NOT NULL, `gid` char(1) DEFAULT NULL, `col1` int(11) DEF ...

  3. 实例讲解Nginx下的rewrite规则

    一.正则表达式匹配,其中:* ~ 为区分大小写匹配* ~* 为不区分大小写匹配* !~和!~*分别为区分大小写不匹配及不区分大小写不匹配二.文件及目录匹配,其中:* -f和!-f用来判断是否存在文件* ...

  4. ArcGIS学习记录—KMZ KML与SHP文件互相转换

      1.在google earth中绘制边界  工具栏中选择"Add Polygon".随意绘制一个多边形.  右击添加的图层名(左侧)保存位置为,选择保存为kmz或kml文件.  ...

  5. 123. Best Time to Buy and Sell Stock III

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  6. autocapticalize和autocorrect

    首字母自动大写autocapitalize 在 iOS 中,用户可以手动开启「首字母自动大写」功能,这样输入英文的时候,首字母便会自动大写.但是,有些时候并不希望一直是首字母大写的.比如用户名这个字段 ...

  7. MySQL高效分页解决方案集

    一,最常见MYSQL最基本的分页方式: select * from content order by id desc limit 0, 10 在中小数据量的情况下,这样的SQL足够用了,唯一需要注意的 ...

  8. JavaScript DOM高级程序设计 3.-DOM2和HTML2--我要坚持到底!

    由一个HTML进行说明,我就不敲了,直接copy <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" " ...

  9. OM Price Lists

      --select * --from org_organization_definitions; --execute fnd_client_info.set_org_context(111); -- ...

  10. Android开发之在子线程中使用Toast

    在子线程中使用Toast的时候,出现Force close. 错误提示:Can't create handler inside thread that has not called Looper.pr ...