Skip to content

Instantly share code, notes, and snippets.

@pdkovacs
Last active May 28, 2021 08:49
Show Gist options
  • Save pdkovacs/288164cde0b7a12e5e1f9ca71a6964f6 to your computer and use it in GitHub Desktop.
Save pdkovacs/288164cde0b7a12e5e1f9ca71a6964f6 to your computer and use it in GitHub Desktop.
Try-catch-finally in so many words
package main
import (
"errors"
"fmt"
)
func foo() (string, error) {
doErr := false
beOpportunist := false
msg := "Hello, playground"
var err error = nil
defer func() {
if err != nil {
fmt.Println("failed with " + msg)
}
}()
if doErr {
err = errors.New("ERROR_FOO")
msg = "Baaaang!!!!"
if beOpportunist {
return "the Opportunist", fmt.Errorf("slipping in: %w", err)
}
}
return msg, err
}
func main() {
msg, err := foo()
if err != nil {
fmt.Printf("Error: %v", err)
return
}
fmt.Printf(msg)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment