FlashKeepers

Go · College · College and University

Go IV: Errors, Testing and Modules

Go error handling, wrapping with errors.Is and As, panic and recover, go test, benchmarks, go modules and tooling.

About this set

42 basic cards for College Go. It opens with "What methods must a type implement to satisfy the error interface?" and "How do you create a simple error using the errors package?". Study it alongside the other College sets below.

42 cards · basic cards · AI-written, checked twice. Edit anything.

Study this set free Look inside first Get FlashKeepers for iPhone

Opens in your browser, no account. Get the iPhone and iPad app · Android app

What methods must a type implement to satisfy the error interface?
Show answerA single Error() method that returns a string
How do you create a simple error using the errors package?
Show answererrors.New("error message")
How do you wrap an error to preserve it for errors.Is and errors.As?
Show answerfmt.Errorf("context: %w", err)
What does errors.Is check?
Show answerWhether an error in the chain matches a target error value
What does errors.As do?
Show answerUnwraps an error chain and assigns the first matching type to a variable
What does errors.Unwrap return?
Show answerThe error wrapped by %w in the argument, or nil if not wrapped
What is a sentinel error?
Show answerA predefined error variable (like var ErrNotFound = errors.New("not found")) used for comparison with errors.Is
How do you create a custom error type?
Show answerDefine a type with an Error() method that returns a string
When should you wrap an error versus returning it unwrapped?
Show answerWrap it to add context; return unwrapped at API boundaries where wrapping adds no value
Name one scenario where panic is appropriate in Go.
Show answerProgrammer error or internal invariant violation (not user input or recoverable failures)
Should invalid user input cause a panic or return an error?
Show answerReturn an error (panic is for unrecoverable programmer errors)
What does recover() do?
Show answerStops a panic and returns the value passed to panic, or nil if not panicking
Where must you call recover() for it to work?
Show answerInside a defer statement
Can recover() in one goroutine catch a panic from a different goroutine?
Show answerNo, recover only catches panics in the same goroutine
What does the go test command do?
Show answerRuns all *_test.go files in the current package and reports results

27 more cards in the app

Study this set free