Skip to content

builtin/error: late assignment of custom error type cause nil checking of the error variable not working #53768

@bsthun

Description

@bsthun

What version of Go are you using (go version)?

$ go version
go version go1.18.3 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/bsthun/Library/Caches/go-build"
GOENV="/Users/bsthun/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Volumes/Data/Environments/gopath/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Volumes/Data/Environments/gopath"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/opt/go/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/opt/go/libexec/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.18.3"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/zc/z09bkbz52kjd29vyb4jjz7rw0000gn/T/go-build613377742=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

After declare an error variable with type of error, and assign its value with a function that returned custom error (a struct with an interface of error). It will have issue when checking nil (err != nil) as the statement will always returnd true even the real value of the variable is nil. You can reproduce the bug using playground: https://go.dev/play/p/otd6w4e7nK1.

What did you expect to see?

The condition statement should return proper value when doing nil check. (if the value of variable err is nil, then err != nil should return false)

What did you see instead?

Even err (which declare as a type error, then later assign to a custom error) has value of nil but the condition err != nil is always returned true.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions