Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

修正数据类型转换相关的一些描述 #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
city2011 wants to merge 18 commits into coderit666:main
base: main
Choose a base branch
Loading
from city2011:main
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
7fbaf7c
(fix)convert str3 (string) to int word fix
city2011 Jan 24, 2022
a796db6
(style)text-align
city2011 Jan 24, 2022
89adc54
(fix) fix some code running result
city2011 Jan 24, 2022
510616c
Merge pull request #1 from city2011/feature-update-some-code
city2011 Jan 24, 2022
2371d93
Merge pull request #2 from city2011/feature-fix-some-description
city2011 Jan 24, 2022
b20143c
update goguide code
Jan 30, 2022
281803e
add chapter map goguide code
Jan 30, 2022
9989aa5
go-guide-code pointer code
Jan 30, 2022
74b57d0
interface test 01
city2011 Feb 3, 2022
ee1342d
go has no extends system
city2011 Feb 4, 2022
b5eaba2
errorPanicTest
city2011 Feb 6, 2022
14e616b
finish strings func study and code
city2011 Feb 7, 2022
df472f1
finish go time reg chapter and code
city2011 Feb 8, 2022
03d0b5b
fileTest half
Feb 11, 2022
16d326a
fileOp in win finish code and learn
city2011 Feb 12, 2022
59587af
fix something in chapter concurrency goruntine
city2011 Feb 13, 2022
f72a394
chan is nb tech in go
city2011 Feb 13, 2022
a618afe
finish chan chapter
city2011 Feb 14, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
errorPanicTest
  • Loading branch information
city2011 committed Feb 6, 2022
commit b5eaba20f2c09944b6e93c58e0bf30ca7c2ae6b5
55 changes: 55 additions & 0 deletions src/goguidecode/errorpanicTest01.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main
import "fmt"

func div(a, b int) (res int) {
// 定义一个延迟调用的函数, 用于捕获panic异常
// 注意: 一定要在panic之前定义
defer func() {
if err := recover(); err != nil{
res = -1
fmt.Println(err) // 除数不能为0
}
}()

if b == 0 {
//err = errors.New("除数不能为0")
panic("除数不能为0")
}else{
res = a / b
}

defer func() {
panic("异常被defer捕获")
}()

return
}

func setValue(arr []int, index int ,value int) {
arr[index] = value
}

func main() {
res := div(10, 0)
fmt.Println(res) // -1

defer func() {
panic("异常被defer捕获")
}()

//panic("异常2")
}

func test2() {
// 如果有异常写在defer中, 并且其它异常写在defer后面, 那么只有defer中的异常会被捕获
defer func() {
if err := recover(); err != nil{
fmt.Println(err) // 异常A
}
}()

defer func() {
panic("异常B")
}()
panic("异常A")
}
31 changes: 31 additions & 0 deletions src/goguidecode/errorpanicTest02.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main
import "fmt"

func test3(a int, b int) {
// 如果有异常写在defer中, 并且其它异常写在defer后面, 那么只有defer中的异常会被捕获
defer func() {
if err := recover(); err != nil{
fmt.Println(err) // 异常A
}
}()

defer func() {
panic("异常B")
}()
panic("异常A")
}

func test1() {
// 多个异常,只有第一个会被捕获
defer func() {
if err := recover(); err != nil{
fmt.Println(err) // 异常A
}
}()
panic("异常A") // 相当于return, 后面代码不会继续执行
panic("异常B")
}

func main() {
test3(10, 0)
}

AltStyle によって変換されたページ (->オリジナル) /