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 ...
随机推荐
- Ubuntu环境下手动配置Java环境
/×××××××××××××××××××××××××××××××××××××××××/ Author:xxx0624 HomePage:http://www.cnblogs.com/xxx0624/ ...
- 【零基础学习iOS开发】【02-C语言】10-函数
前面已经讲完了C语言中的基本语句和基本运算了,这讲呢,介绍C语言中的重头戏---函数.其实函数这个概念,在大部分高级语言中都是非常重要的,我也已经在<第一个C语言程序>一讲中对函数作了一个 ...
- 单独下载的Qt library则一般不带SSL(包括QT FAQ)
http://www.cnblogs.com/E7868A/archive/2012/11/15/2771501.html http://www.oldcai.com/archives/208 htt ...
- VS下遇到未能加载文件或程序集 错误
这个的错误原因可能是在64的系统上编译32位的应用程序,遇到这个错误,可以通过下面的手段解决! 1.关闭Visual Studio. 2. 在Visual Studio Tools子目录,以管理员身份 ...
- ADT开发中的一些优化设置:代码背景色、代码字体大小、代码自动补全
初学Android开发,在网上找到一些ADT工具的优化,自己设置好了,截图保存下来.免得以后忘了. 1. 设置背景颜色: 色调85.饱和度90.亮度205 RGB:199.237.204 2. 设置代 ...
- linux 查看系统信息命令
linux 查看系统信息命令是linux初学者必备的基础知识, 这些命令也非常有用, 因为进入linux第一件事就可能是首先查看系统信息, 因此必要的系统的学习一下这些linux系统信息命令还是非常有 ...
- gdb调试SAPI方式的php
一.修改php-fpm.conf文件 /usr/local/php/etc/php-fpm.conf pm.max_children = 1 #只产生一个进程,便于追踪 二.得到进行服务的进程号 [r ...
- JS 动态显示 获取下拉框的多个值
<script type="text/javascript"> function GetProcessVal(i, t) { document.getElementsB ...
- [LOJ 1030] Discovering Gold
B - Discovering Gold Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- 如何卸载eclipse中的ADT
卸载ADT的方法,方法如下: 1.选择 Help > Install New Software: 2.在"Details" 面板中, 点击"What is alre ...