GOLANG

Error handling in Go

Go does not provide conventional try/catch method to handle the errors, instead, errors are returned as a normal return value. In this article, we are going to explore error handling in Go.

Uday Hiwarale
Jun 12, 2019 · 13 min read
Image for post
Image for post
(source: pexels.com)
val, err := myFunction( args... );if err != nil {
// handle error
} else {
// success
}
type error interface {
Error() string
}
Image for post
Image for post
https://play.golang.org/p/ciha7WtKbMt
Image for post
Image for post
https://play.golang.org/p/-U5aCAfHdlF
Image for post
Image for post
https://play.golang.org/p/oP9OGjFl1pB
type errorString struct {
s string
}

func (e *errorString) Error() string {
return e.s
}
func New(text string) error {
return &errorString{text}
}
Image for post
Image for post
https://play.golang.org/p/lEy_bK1NIHO

Adding context to an error / custom errors

Image for post
Image for post
https://play.golang.org/p/KCqSzD_gwvd
Image for post
Image for post
https://play.golang.org/p/98X2mHVYzE-

Saving original error as a context (wrapping an error)

Image for post
Image for post
https://play.golang.org/p/7V7QPco1xb3
Image for post
Image for post
https://play.golang.org/p/HN3rThDeKLi

Getting error context using methods

Image for post
Image for post
https://play.golang.org/p/GgKL3Z4ZuLu

Custom error interfaces

Image for post
Image for post
https://play.golang.org/p/8yndc45QPbC

Adding stack-trace to an error

Image for post
Image for post
https://play.golang.org/p/jNaTbCT9EcV

Improve error handling

Handle errors like you mean it

val, _ := someFunctionWhichCanReturnAnError();

Don’t just return an error

func someFunction() error {
val, err := someFunctionWhichCanReturnAnError();
if err != nil {
return err
} else {
// proceed further
}
}

Don’t handle error multiple times

func someFunction() error {
if err != nil {
// log error
// return err
}
}
func someOtherFunction() error {
val, err := someFunction()
if err != nil {
// log error
// return err
}
}

Assert errors for the behavior, not type

Image for post
Image for post

RunGo

A go-to guide for learning Go programming language

Uday Hiwarale

Written by

Senior Software Engineer / thatisuday.com ☯ github.com/thatisuday ☯ thatisuday@gmail.com

RunGo

RunGo

A place to find introductory Go programming language tutorials and learning resources. In this publication, we will learn Go in an incremental manner, starting from beginner lessons with mini examples to more advanced lessons.

Uday Hiwarale

Written by

Senior Software Engineer / thatisuday.com ☯ github.com/thatisuday ☯ thatisuday@gmail.com

RunGo

RunGo

A place to find introductory Go programming language tutorials and learning resources. In this publication, we will learn Go in an incremental manner, starting from beginner lessons with mini examples to more advanced lessons.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store