Last active
May 28, 2021 08:49
-
-
Save pdkovacs/288164cde0b7a12e5e1f9ca71a6964f6 to your computer and use it in GitHub Desktop.
Try-catch-finally in so many words
This file contains 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
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