Errors Flashcards

1
Q

What is the last return value in a Go function that returns an error?

A

The last return value is of type error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does a nil error value indicate in Go?

A

It indicates that the function worked as expected.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you check for errors in Go?

A

Compare the error value to nil.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the Go built-in interface for errors?

A

The error interface with the method Error() string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does errors.New do in Go?

A

It creates an error with a string message.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the purpose of fmt.Errorf in Go?

A

It creates an error with formatted runtime info, using fmt.Printf verbs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a sentinel error in Go?

A

A predefined error used to signal specific processing issues, usually declared at the package level.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you check for a sentinel error in Go?

A

Use == to compare the returned error with a sentinel error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Can you define custom error types in Go?

A

Yes, custom error types can be defined, implementing the Error() method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you preserve errors with additional information in Go?

A

Use fmt.Errorf with the %w verb to wrap errors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can you access the wrapped error in Go?

A

Use the errors.Unwrap() function to unwrap and access the original error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is required for custom errors to support unwrapping?

A

Custom errors must implement the Unwrap() method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you combine multiple errors into one in Go?

A

Use errors.Join() to merge multiple errors into a single error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does errors.Is do in Go?

A

It checks if a specific error is present, including in wrapped errors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the difference between errors.Is and errors.As in Go?

A

errors.Is checks for specific error instances, while errors.As checks for a specific error type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What happens when a panic occurs in Go?

A

The program exits, and a stack trace is printed.

17
Q

How do you handle a panic in Go?

A

Use recover() in a defer block to handle the panic and continue execution.

18
Q

What is the recommended approach for handling errors instead of using panic and recover?

A

Use error wrapping to include stack traces, instead of relying on panic and recover.