- 
  Notifications
 
You must be signed in to change notification settings  - Fork 343
 
修正数据类型转换相关的一些描述 #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
 
 
 
 
  Open
 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 a796db6
 
 (style)text-align
 
 
 city2011 89adc54
 
 (fix) fix some code running result
 
 
 city2011 510616c
 
 Merge pull request #1 from city2011/feature-update-some-code
 
 
 city2011 2371d93
 
 Merge pull request #2 from city2011/feature-fix-some-description
 
 
 city2011 b20143c
 
 update goguide code
 
 
 281803e
 
 add chapter map goguide code
 
 
 9989aa5
 
 go-guide-code pointer code
 
 
 74b57d0
 
 interface test 01
 
 
 city2011 ee1342d
 
 go has no extends system
 
 
 city2011 b5eaba2
 
 errorPanicTest
 
 
 city2011 14e616b
 
 finish strings func study and code
 
 
 city2011 df472f1
 
 finish go time reg chapter and code
 
 
 city2011 03d0b5b
 
 fileTest half
 
 
 16d326a
 
 fileOp in win finish code and learn
 
 
 city2011 59587af
 
 fix something in chapter concurrency goruntine
 
 
 city2011 f72a394
 
 chan is nb tech in go
 
 
 city2011 a618afe
 
 finish chan chapter
 
 
 city2011 File filter
Filter by extension
Conversations
 Failed to load comments. 
 
 
 
  Loading
 
 Jump to
 
 Jump to file
 
 
 
 Failed to load files. 
 
 
 
  Loading
 
 Diff view
Diff view
 errorPanicTest
 
 - Loading branch information
 
There are no files selected for viewing
 
 
 
 55 changes: 55 additions & 0 deletions
 
 
 
 src/goguidecode/errorpanicTest01.go
 
 
 
 
  
 
 
 
 
 
 
 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
 Learn more about bidirectional Unicode characters
 
 
 
 
 | 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
 
 
 
 
  
 
 
 
 
 
 
 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
 Learn more about bidirectional Unicode characters
 
 
 
 
 | 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) | ||
| } | 
 Add this suggestion to a batch that can be applied as a single commit.
 This suggestion is invalid because no changes were made to the code.
 Suggestions cannot be applied while the pull request is closed.
 Suggestions cannot be applied while viewing a subset of changes.
 Only one suggestion per line can be applied in a batch.
 Add this suggestion to a batch that can be applied as a single commit.
 Applying suggestions on deleted lines is not supported.
 You must change the existing code in this line in order to create a valid suggestion.
 Outdated suggestions cannot be applied.
 This suggestion has been applied or marked resolved.
 Suggestions cannot be applied from pending reviews.
 Suggestions cannot be applied on multi-line comments.
 Suggestions cannot be applied while the pull request is queued to merge.
 Suggestion cannot be applied right now. Please check back later.