How to Use errors.As in Go

Use errors.As to check if an error matches a specific type and extract its concrete value for detailed handling.

Use errors.As to check if an error value (or any error in its chain) matches a specific type, allowing you to access type-specific fields. It returns true if the target is found and assigns the concrete error value to the target pointer.

var target *tar.HeaderError
if errors.As(err, &target) {
    // err is a *tar.HeaderError; access target.Error() or fields
}