Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: value != nil returning true for nil values. #43643

Closed
hsblhsn opened this issue Jan 12, 2021 · 2 comments
Closed

BUG: value != nil returning true for nil values. #43643

hsblhsn opened this issue Jan 12, 2021 · 2 comments

Comments

@hsblhsn
Copy link

hsblhsn commented Jan 12, 2021

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

$ go version
go version go1.15.6 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=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/lab44/Library/Caches/go-build"
GOENV="/Users/lab44/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/lab44/go/pkg/mod"
GOOS="darwin"
GOPATH="/Users/lab44/go"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.15.6/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.15.6/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/15/_y19tt655g11nh3h3b25qj100000gp/T/go-build109875245=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

https://play.golang.org/p/rcCFGJNhHYh

My code
package main

import (
"encoding/json"
"fmt"
"log"
"reflect"

"github.com/fatih/structs"

)

func main() {
type requestBody struct {
Title *string
Description *string
ProductType *string
Brand *string
}

content := []byte(`{"description": "Hello-World"}`)
var result requestBody

err := json.Unmarshal(content, &result)
if err != nil {
	log.Fatal(err)
}

fields := structs.Map(result)
updateFields := make(map[string]interface{})
for name, value := range fields {
	if value != nil {
		// no nil values should be printed here
		// Because we already checked nil values in line 31.
		// But why it's printing the value?
		//
		//
		// ORIGINAL OUTPUT:
		// Title - (*string)(nil)
		// Description - (*string)(0xc00010a140)
		// ProductType - (*string)(nil)
		// Brand - (*string)(nil)
		//
		// OUTPUT I WANT:
		// Description - (*string)(0xc00010a140)
		//
		fmt.Printf("%v - %#v\n", name, value)
		updateFields[name] = reflect.Indirect(reflect.ValueOf(value))
	}
}

}

What did you expect to see?

Description - (*string)(0xc00010a140)

What did you see instead?

Title - (*string)(nil)
Description - (*string)(0xc00010a140)
ProductType - (*string)(nil)
Brand - (*string)(nil)
@davecheney
Copy link
Contributor

Thank you for this issue, this is not a bug in Go, it's a side effect of the type stored in the interface value.

There is an FAQ entry for this https://golang.org/doc/faq#nil_error (it's about errors, but applies to any interface value)

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For asking questions, see:

Closing as this is not a bug.

@hsblhsn
Copy link
Author

hsblhsn commented Jan 12, 2021

Thank you for your response. I have fixed my issue.

In case anyone is looking for the solution:

reflect.ValueOf(value).IsNil() fixed my issue.

	fields := structs.Map(requestData)
	updateFields := make(map[string]interface{})
	for name, value := range fields {
		fieldVal := reflect.ValueOf(value)
		if !fieldVal.IsNil() {
			updateFields[name] = reflect.Indirect(fieldVal).Interface()
		}
	}

Thank you!

@golang golang locked and limited conversation to collaborators Jan 12, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants