当前位置:网站首页>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
边栏推荐
- MBA-day5數學-應用題-工程問題
- Software testers, how to mention bugs?
- Mysql8.0安装指南
- 26. 删除有序数组中的重复项
- Xdotool key Wizard
- 比深度学习更值得信赖的模型ART
- JDBC – PreparedStatement – 如何设置 Null 值?
- Implementation of inserting millions of data into MySQL database in 10 seconds
- 语雀文档编辑器将开源:始于但不止于Markdown
- Facing the global market, platefarm today logs in to four major global platforms such as Huobi
猜你喜欢
Visual common drawing (III) area map
Visualization Road (10) detailed explanation of segmentation canvas function
Promise details
Solution architect's small bag - 5 types of architecture diagrams
MIT:用无监督为世界上每个像素都打上标签!人类:再也不用为1小时视频花800个小时了
Promise详解
Visual common drawing (V) scatter diagram
Use of SVN:
Jupyter lab top ten high productivity plug-ins
More reliable model art than deep learning
随机推荐
妊娠箱和分娩箱的区别
Visual solutions to common problems (VIII) mathematical formulas
colab
学习 Go 语言 0x04:《Go 语言之旅》中切片的练习题代码
VIM usage
比深度学习更值得信赖的模型ART
MBA-day5數學-應用題-工程問題
Upgrade the functions available for cpolar intranet penetration
Mba-day5 Mathematics - application problems - engineering problems
CUMCM 2021-b: preparation of C4 olefins by ethanol coupling (2)
Mba-day6 logic - hypothetical reasoning exercises
mysql创建存储过程及函数详解
Typora operation skill description (I) md
Detailed explanation of writing sequence and execution sequence of MySQL series SQL query statements
After the MySQL router is reinstalled, it reconnects to the cluster for boot - a problem that has been configured in this host before
Visual common drawing (IV) histogram
Understand the key points of complement
如何使用JDBC CallableStatement.wasNull()方法调用来查看最后一个OUT参数的值是否为 SQL NULL
Introduction to neo4j authoritative guide, recommended by Qiu Bojun, Zhou Hongxiang, Hu Xiaofeng, Zhou Tao and other celebrities
Software testers, how to mention bugs?