当前位置:网站首页>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
边栏推荐
- Mysql排序的特性详情
- 防止web项目中的SQL注入
- Visual common drawing (IV) histogram
- remote: Support for password authentication was removed on August 13, 2021.
- Data analysis learning (I) data analysis and numpy Foundation
- 我的创作纪念日
- Typora operation skill description (I)
- MySQL partition table can be classified by month
- Xdotool key Wizard
- Excel · VBA array bubble sorting function
猜你喜欢

Cygwin 中的 rename 用法

Solutions to common problems in visualization (IX) background color

Source insight 4.0 FAQs

Jupyter Lab 十大高生产力插件

After the MySQL router is reinstalled, it reconnects to the cluster for boot - a problem that has been configured in this host before

Constraintlayout layout

Visual solutions to common problems (VIII) mathematical formulas

Promise details

CUMCM 2021-B:乙醇偶合制备C4烯烃(2)

Go interface usage
随机推荐
CUMCM 2021-b: preparation of C4 olefins by ethanol coupling (2)
MySQL面试题讲解之如何设置Hash索引
mysql分表之后如何平滑上线详解
Constraintlayout layout
Visual solutions to common problems (VIII) mathematical formulas
More reliable model art than deep learning
Latex usage
mysql插入datetime类型字段不加单引号插入不成功
MySQL interview questions explain how to set hash index
精彩回顾|「源」来如此 第六期 - 开源经济与产业投资
Visualized common drawing (II) line chart
Detailed explanation of how to smoothly go online after MySQL table splitting
How to bind a process to a specified CPU
学习 Go 语言 0x03:理解变量之间的依赖以及初始化顺序
redis优化系列(二)Redis主从原理、主从常用配置
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 (I) stacking diagram
Detailed explanation of integer data type tinyint in MySQL
Promise details
期货开户哪个公司好?安全靠谱的期货公司谁能推荐几家?