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. HDU1431+简单题

    题意简单 预处理之后会发现符合条件的数最多781个... 所以打表.. /* */ #include<algorithm> #include<iostream> #includ ...

  2. JavaScript代码调试

    怎么在浏览器中调试JavaScript代码呢?首先,你需要安装Google Chrome浏览器,Chrome浏览器对开发者非常友好,可以让你方便地调试JavaScript代码.安装后,随便打开一个网页 ...

  3. Android 使用split函数进行多个空格分割

    在项目中经常会遇到按字符分割字符串的情况,可以使用String对象的split函数进行分割. 先看实际情况: String str = "关键词1 关键词2 关键词3"; Stri ...

  4. 捕获Java线程池执行任务抛出的异常

    捕获Java线程池执行任务抛出的异常Java中线程执行的任务接口java.lang.Runnable 要求不抛出Checked异常, public interface Runnable { publi ...

  5. How to change data dir of mysql?

    # 1 copy orgin data dir of mysql to new one cp -R /var/lib/mysql /mysqldata chown mysql:mysql -R /my ...

  6. [转] android自动化之MonkeyRunner测试环境配置(一)

    Android自动化测试之MonkeyRunner 一.Android自动化测试之环境搭建 1.1  Android-sdk介绍 ¢ SDK(Software development kit)软件开发 ...

  7. poj 1840 Eqs (hash)

    题目:http://poj.org/problem?id=1840 题解:http://blog.csdn.net/lyy289065406/article/details/6647387 小优姐讲的 ...

  8. [swustoj 371] 回文数

    回文数(0371) 问题描述 一个自然数如果把所有数字倒过来以后和原来的一样,那么我们称它为回文数.例如151和753357.我们可以把所有回文数从小到大排成一排:1, 2, 3, 4, 5, 6, ...

  9. $http.post发的数据,后台取不到两种解决方案

    方案一: var url = 'Gulugulus/setMenu', data = { menu: JSON.stringify(menu), test: 'a String' }, transFn ...

  10. 算法的时间复杂度(大O表示法)

    定义:如果一个问题的规模是n,解这一问题的某一算法所需要的时间为T(n),它是n的某一函数 T(n)称为这一算法的“时间复杂性”. 当输入量n逐渐加大时,时间复杂性的极限情形称为算法的“渐近时间复杂性 ...