当前位置:网站首页>Learning go language 0x08: practice using error in go language journey
Learning go language 0x08: practice using error in go language journey
2022-04-23 11:08:00 【Love blog uncle】
* subject
https://tour.go-zh.org/methods/20
practice : error
from Previous exercises Copy the Sqrt function , Modify it to return error
value .
Sqrt When a negative number is received , A non should be returned nil The wrong value of . The plural is also not supported .
Create a new type
type ErrNegativeSqrt float64
And for it
func (e ErrNegativeSqrt) Error() string
Method to have error
value , adopt ErrNegativeSqrt(-2).Error()
Calling this method should return "cannot Sqrt negative number: -2"
.
Be careful : stay Error Method fmt.Sprint(e)
It's going to put the program in a dead cycle . You can do this by first converting e To avoid this problem :fmt.Sprint(float64(e))
. Why is that ?
modify Sqrt
function , To accept a negative number , return ErrNegativeSqrt
value .
* Code
package main
import (
"fmt"
)
type ErrNegativeSqrt float64
func (e ErrNegativeSqrt) Error() string {
return fmt.Sprintf("cannot Sqrt negative number: %v", float64(e))
}
func Sqrt(x float64) (float64, error) {
if x < 0 {
return 0, ErrNegativeSqrt(x)
}
z := 1.0
for i := 1; i < 10; i++ {
z -= (z*z - x) / (2*z)
}
return z, nil
}
func main() {
fmt.Println(Sqrt(2))
fmt.Println(Sqrt(-2))
}
Output :
1.4142135623730951 <nil>
0 cannot Sqrt negative number: -2
* take fmt.Sprint(float64(e))
Change it to fmt.Sprint(e)
, The stack will overflow
If hold fmt.Sprintf("cannot Sqrt negative number: %v", float64(e))
Change to fmt.Sprintf("cannot Sqrt negative number: %v", e)
, The running result is :
1.4142135623730951 <nil>
runtime: goroutine stack exceeds 250000000-byte limit
fatal error: stack overflow
runtime stack:
runtime.throw(0x1054d9, 0xe)
/usr/local/go/src/runtime/panic.go:617 +0x80
runtime.newstack()
/usr/local/go/src/runtime/stack.go:1041 +0x960
runtime.morestack()
/usr/local/go/src/runtime/asm_amd64p32.s:299 +0xc0
goroutine 1 [running]:
sync.(*Mutex).Lock(0x3422094, 0x0)
/usr/local/go/src/sync/mutex.go:72 +0x3a0 fp=0x8c00360 sp=0x8c00358 pc=0x99b80
sync.(*Pool).getSlow(0x1ad078, 0x8, 0x3422080, 0x0)
/usr/local/go/src/sync/pool.go:165 +0xe0 fp=0x8c00390 sp=0x8c00360 pc=0x9a400
sync.(*Pool).Get(0x1ad078, 0x1b4540, 0xfefc0004, 0x0)
/usr/local/go/src/sync/pool.go:141 +0x1a0 fp=0x8c003b8 sp=0x8c00390 pc=0x9a2a0
fmt.newPrinter(0x8c003f0, 0x0)
/usr/local/go/src/fmt/print.go:134 +0x40 fp=0x8c003d0 sp=0x8c003b8 pc=0xce6e0
fmt.Sprintf(0x1082ef, 0x1f, 0x8c00420, 0x1, 0x1, 0x8c00438, 0x18, 0x139f)
/usr/local/go/src/fmt/print.go:213 +0x20 fp=0x8c00400 sp=0x8c003d0 pc=0xcec40
main.ErrNegativeSqrt.Error(...)
/tmp/sandbox804641635/main.go:10
main.(*ErrNegativeSqrt).Error(0x67a3330, 0x122ecc, 0x67c7300, 0xf1b40)
<autogenerated>:1 +0xc0 fp=0x8c00430 sp=0x8c00400 pc=0xd97c0
fmt.(*pp).handleMethods(0x67c7300, 0x76, 0x6cd01, 0x139f)
/usr/local/go/src/fmt/print.go:610 +0x1e0 fp=0x8c00480 sp=0x8c00430 pc=0xd2480
fmt.(*pp).printArg(0x67c7300, 0xf1b40, 0x67a3330, 0x76)
/usr/local/go/src/fmt/print.go:699 +0x260 fp=0x8c004d0 sp=0x8c00480 pc=0xd2d00
fmt.(*pp).doPrintf(0x67c7300, 0x1082ef, 0x1f, 0x8c00598, 0x1, 0x1)
/usr/local/go/src/fmt/print.go:1016 +0x180 fp=0x8c00548 sp=0x8c004d0 pc=0xd7640
fmt.Sprintf(0x1082ef, 0x1f, 0x8c00598, 0x1, 0x1, 0x8c005b0, 0x18, 0x139f)
/usr/local/go/src/fmt/print.go:214 +0x60 fp=0x8c00578 sp=0x8c00548 pc=0xcec80
main.ErrNegativeSqrt.Error(...)
/tmp/sandbox804641635/main.go:10
A little ( There are still a lot of it log)
An understanding of code looping :
Code in question , as follows :
func (e ErrNegativeSqrt) Error() string {
return fmt.Sprintf("cannot Sqrt negative number: %v", e)
}
First execution ErrNegativeSqrt
Of Error()
function , By main
Function fmt.Println(Sqrt(-2))
The trigger .Sqrt(-2)
return error by ErrNegativeSqrt
, stay main
Print this in error, Will execute ErrNegativeSqrt
Of Error()
function .
And then in Error()
Internal function , perform fmt.Sprintf("cannot Sqrt negative number: %v", e)
,e
by ErrNegativeSqrt
, therefore , Print again ErrNegativeSqrt
, therefore , It also calls ErrNegativeSqrt
Of Error()
function . So there's a dead cycle , Until the call stack overflows and an error is reported .
版权声明
本文为[Love blog uncle]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231102105832.html
边栏推荐
- Typora operation skill description (I) md
- Resolution and size of mainstream mobile phones
- Ueditor -- limitation of 4m size of image upload component
- Mba-day5 Mathematics - application problems - engineering problems
- 如何使用JDBC CallableStatement.wasNull()方法调用来查看最后一个OUT参数的值是否为 SQL NULL
- Implementation of inserting millions of data into MySQL database in 10 seconds
- SVN的使用:
- 采用百度飞桨EasyDL完成指定目标识别
- Mba-day5 Mathematics - application problems - engineering problems
- Mysql中有关Datetime和Timestamp的使用总结
猜你喜欢
GO接口使用
Constraintlayout layout
CUMCM 2021-B:乙醇偶合制备C4烯烃(2)
升级cpolar内网穿透能获得的功能
Visual common drawing (IV) histogram
ConstraintLayout布局
Solutions to common problems in visualization (IX) background color
Structure of C language (Advanced)
About the three commonly used auxiliary classes of JUC
Jupyter lab top ten high productivity plug-ins
随机推荐
学习 Go 语言 0x01:从官网开始
Canvas详解
Mysql中一千万条数据怎么快速查询
Mysql中有关Datetime和Timestamp的使用总结
Differences among restful, soap, RPC, SOA and microservices
妊娠箱和分娩箱的区别
mysql分表之后如何平滑上线详解
学习 Go 语言 0x03:理解变量之间的依赖以及初始化顺序
MySQL interview questions explain how to set hash index
Oracle连通性测试小工具
MBA-day5数学-应用题-工程问题
Typora operation skill description (I)
How to Ping Baidu development board
Mba-day5 Mathematics - application problems - engineering problems
colab
Google Earth Engine(GEE)——将原始影像进行升尺度计算(以海南市为例)
Excel · VBA array bubble sorting function
VIM usage
Promise details
Strongest date regular expression