Skip to content

ghosind/go-try

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-try

test Go Report Card codecov Version Badge License Badge Go Reference

The try...catch statement alternative in Golang.

Installation

go get -u github.com/ghosind/go-try

Getting Started

There is the simplest example to run a function and handle the error in the catch function that the try function returned.

out, err := try.TryCatch(func () (error) {
  // Do something
  return err
}, func (err error) {
  // The function will not executing if err is nil.
  // Handle error...
})

The try functions will return two values. The first value is the all values the try function returned, and the second value is the error that the try function returns or it panics.

out, err := try.Try(func () (string, error) {
  return "Hello world", errors.New("expected error")
})
fmt.Println(out)
fmt.Println(err)
// [Hello world, expected error]
// expected error

The package provides four forms for the try statement alternative:

  • Try
  • TryFinally
  • TryCatch
  • TryCatchFinally

In default cases, the try functions will catch the error that the try function panics, and you can set the CatchPanic variable to false to disable it.

try.CatchPanic = false
try.Try(func () {
  panic("panic error")
})
// panic: expected error