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. 关于DJANGO和JAVASCRIPT的时间

    最近,实际一些简单统计时,要到库里去检索数据出来用HIGHCHARTS画图, 作一个简单的回照.. DJANGO用TEMPLATEVIEW来作.专业,正规:) class SAView(Templat ...

  2. 【BZOJ 2618】 2618: [Cqoi2006]凸多边形 (半平面交)

    2618: [Cqoi2006]凸多边形 Description 逆时针给出n个凸多边形的顶点坐标,求它们交的面积.例如n=2时,两个凸多边形如下图: 则相交部分的面积为5.233. Input 第一 ...

  3. Web缓存杂谈--Etag & If-None-Match

    一.概述 缓存通俗点,就是将已经得到的‘东东’存放在一个相对于自己而言,尽可能近的地方,以便下次需要时,不会再二笔地跑到起始点(很远的地方)去获取,而是就近解决,从而缩短时间和节约金钱(坐车要钱嘛). ...

  4. LINUX ulimit命令

    概述 系统性能一直是一个受关注的话题,如何通过最简单的设置来实现最有效的性能调优,如何在有限资源的条件下保证程序的运作,ulimit 是我们在处理这些问题时,经常使用的一种简单手段.ulimit 是一 ...

  5. matlab 怎么保存plot的图 到指定文件夹

    %%使用print函数,第一个参数一定是figure的句柄,第二个参数设置格式,第三个参数是指定文件夹 %代码如下 h=figure; plot(1:10); print(h,'-djpeg','F: ...

  6. zookeeper入门必读

    (如果感觉有帮助,请帮忙点推荐,添加关注,谢谢!你的支持是我不断更新文章的动力.本博客会逐步推出一系列的关于大型网站架构.分布式应用.设计模式.架构模式等方面的系列文章) 今天我想谈谈zookeepe ...

  7. IMX51+WINCE6.0平台缩写意义

    1.以EPIT为例 EPIT(Enhanced Periodic Interrupt Timer)为增强型周期中断定时器,其中有CR控制寄存器,要设置CR寄存器的SWR位,代码如下: // Asser ...

  8. HTML5学习(六)---------SVG 与Canvas

    参考教程:http://www.w3school.com.cn/html5/html_5_canvas_vs_svg.asp Canvas 和 SVG 都允许您在浏览器中创建图形,但是它们在根本上是不 ...

  9. UVa 753 (二分图最大匹配) A Plug for UNIX

    题意: 有n个插座,m个设备以及k种转化器(每种转化器视为有无限个). 转换器A->B可以将A类型的插头转化成B类型的插头,所以可以插在B类型的插座上. 求最少剩多少不匹配的设备. 分析: 抛开 ...

  10. WordPress 3.8.1 /xmlrpc.php拒绝服务漏洞

    漏洞版本: WordPress 3.8.1 漏洞描述: WordPress是一款内容管理系统. WordPress 3.8.1 /xmlrpc.php 文件有ping其他主机的功能,通过这个功能可以请 ...