Go - Error. Unable to unpack custom error type
php editor Xigua introduces to you: In Go language, when we use custom error types, we sometimes encounter "Error. Unable to unpack custom error type" question. This problem usually occurs when we try to pass custom error types to other functions or methods. While this may seem like a tricky problem, there are actually several ways to go about it. In this article, we will explore the causes of this problem and provide solutions to help you solve this problem.
Question content
I'm trying to use the go stdlib package errors to unpack a custom error type using errors.as
, but it seems the check is failing and I can't extract the underlying error .
I extracted a minimal reproducible example:
package main import ( "errors" "fmt" ) type myError struct { err error } func (m myError) Error() string { return fmt.Sprintf("my error: %s", m.err) } func retError() error { return &myError{errors.New("wrapped")} } func main() { var m myError if err := retError(); errors.As(err, &m) { fmt.Println("unwrapped", m.err) } else { fmt.Println(err) } }
https://go.dev/play/p/i7bnk4-rdib - Examples on the go playground. If launched, it prints "my error: wrapping" instead of the expected "unwrapped wrapping".
The example in theerrors.as
documentation works, I can't seem to understand what I'm doing wrong - I pass *myerror
to errors.as
and that seems to be Correct (because passing myerror
causes a panic: target must be a non-zero pointer
, which is expected). Solution
instead of:
func reterror() error { return &myerror{errors.new("wrapped")} }
Do:
func retError() error { return myError{errors.New("wrapped")} }
The above is the detailed content of Go - Error. Unable to unpack custom error type. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

When using sql.Open, why doesn’t the DSN report an error? In Go language, sql.Open...
