Closed
Description
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 mainimport (
"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)