Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/josephbuchma/calmly
golang try - catch - finally
https://github.com/josephbuchma/calmly
exception-handler golang panic
Last synced: about 2 months ago
JSON representation
golang try - catch - finally
- Host: GitHub
- URL: https://github.com/josephbuchma/calmly
- Owner: josephbuchma
- Created: 2015-12-01T06:36:01.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2016-02-06T11:40:47.000Z (almost 9 years ago)
- Last Synced: 2024-04-23T23:26:23.088Z (9 months ago)
- Topics: exception-handler, golang, panic
- Language: Go
- Size: 2.93 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Calmly
[![Build Status](https://travis-ci.org/JosephBuchma/calmly.svg?branch=master)](https://travis-ci.org/JosephBuchma/calmly)Package calmly is example of golang package that should not exist.
```go
import (
"fmt"
"github.com/JosephBuchma/calmly"
)type MyError struct{}
type MyAnotherError struct{}func main(){
calmly.Try(func(){
panic(MyError{}) // `raise` error (any type)// Ideally you should pass into Catch only type, (e.g. `MyError`, not `MyError{}`),
// but such syntax is not supported by golang yet.
// Discussion is here https://groups.google.com/forum/#!topic/golang-nuts/dYMlhyq5FpA
}).Catch(MyError{}, func(e calmly.E){
fmt.Println("Error catched")// Add as many catches as you need...
}).Catch(MyAnotherError{}, func(e calmly.E) {
fmt.Println("Not this time")// e.g. `catch (...)`
}).CatchAny(func(e calmly.E) {
fmt.Println("Not this time")// Finally runs anyway. Finally is required clause.
// If you don't need any finalization, simply pass nil as parameter instead of func().
}).Finally(func(){
fmt.Println("Finalization")
})
}
```See calmly_test.go for more examples.