A Tour of Go Exercise: Errors
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的更多相关文章
- A Tour of Go Exercise: Images
Remember the picture generator you wrote earlier? Let's write another one, but this time it will ret ...
- A Tour of Go Exercise: HTTP Handlers
Implement the following types and define ServeHTTP methods on them. Register them to handle specific ...
- A Tour of Go Exercise: Fibonacci closure
Let's have some fun with functions. Implement a fibonacci function that returns a function (a closur ...
- 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 ...
- 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 ...
- 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 ...
- exercise.tour.go google的go官方教程答案
/* Exercise: Loops and Functions #43 */ package main import ( "fmt" "math" ) fun ...
- 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 ...
- A Tour of Go Errors
An error is anything that can describe itself as an error string. The idea is captured by the predef ...
随机推荐
- win8.1下使用vmware workstation 来编译ffmpeg
先吐槽下,T440预装的win8.1 采用 UEFI+GPT分区导致 无法使用传统的EasyBCD来达到win+ubuntu双系统效果, 尝试了2天全部失败,等以后有时间了 买块U盘再干掉win8 现 ...
- MyBatis的动态SQL操作--更新
更新条件不确定,需要根据具体的情况生成sql语句. id是主键,一般不会去更新. 1.只更新name的值 update student set name = ? where id = ? 2.只更新s ...
- HDU4907——Task schedule(BestCoder Round #3)
Task schedule Description有一台机器,并且给你这台机器的工作表,工作表上有n个任务,机器在ti时间执行第i个任务,1秒即可完成1个任务.有m个询问,每个询问有一个数字q,表示如 ...
- mysql 闪回表工具
use HTTP::Date qw(time2iso str2time time2iso time2isoz); use POSIX; my $SDATE = strftime("%Y-%m ...
- Extension Method[上篇]
在C#3.0中,引入了一些列新的特性,比如: Implicitly typed local variable, Extension method,Lambda expression, Object i ...
- IMX51启动模式
相关链接: http://blog.csdn.net/kickxxx/article/details/7236040 http://blog.csdn.net/evilcode/article/det ...
- Android开发之AIDL的使用一--跨应用启动Service
启动其他App的服务,跨进程启动服务. 与启动本应用的Service一样,使用startService(intent)方法 不同的是intent需要携带的内容不同,需要使用intent的setComp ...
- Ubuntu安装node.js
通过PPA安装Node.js sudo apt-get install python-software-properties sudo add-apt-repository ppa:chris-lea ...
- 1126. Magnetic Storms(单调队列)
1126 最简单的单调队列应用吧 单调队列是指在一个队列中各个元素单调 递增(或者递减),并且各个元素的下标单调 递增. 单调队列的大体操作 进队时,将进队的元素为e,从队尾往前扫描,直到找到一个不大 ...
- poj1180
斜率优化dp 据说这题朴素的O(n2)dp也可以A 没试过 朴素的dp不难想:f[i]=min(f[j]+sumtime[i]*sumcost[j+1,i]+c*sumcost[j+1,n]) 稍微解 ...