Skip to content
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

Added Go defer and panic control flow mechanism #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
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
41 changes: 40 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
17. [Printing](#printing)
18. [Snippets](#snippets)
* [Http-Server](#http-server)
19. [Defer And Panic](#defer-panic)


## Credits

Expand Down Expand Up @@ -406,7 +408,8 @@ var m = map[string]Vertex{

```

## Structs
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. Sorry about this. I will add the recover func and update the PR. Thanks

##


There are no classes, only structs. Structs can have methods.
```go
Expand Down Expand Up @@ -669,3 +672,39 @@ func main() {
// ServeHTTP(w http.ResponseWriter, r *http.Request)
// }
```

## Defer And Panic
Defer statements pushes a function call onto a stack. All the function calls are popped and executed after the surrounding functions returns.
```go
func deferexample() {
for i := 0; i < 4; i++ {
defer fmt.Println(i)
}
}
// It will print 3, 2, and 1.
```
Panic statements is used in Go to report something which went unexpectedly wrong. We use panic to denote the unexpected errors.

```go
package main
import "os"

func main() {
panic(" a problem")

_, err := os.Create("/tmp/file")
if err != nil {
panic(err)
}
}

//If we use go run panic.go and if we get an unexpected error when creating a new file
//the output will be
// panic: a problem

// goroutine 1 [running]:
// main.main()
// /.../panic.go:12 +0x47
// ...
// exit status 2
```