Skip to content

reflect.ValueOf: unexpected behavior #71724

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

Closed
LayTsyn opened this issue Feb 13, 2025 · 2 comments
Closed

reflect.ValueOf: unexpected behavior #71724

LayTsyn opened this issue Feb 13, 2025 · 2 comments
Labels
BugReport Issues describing a possible bug in the Go implementation.

Comments

@LayTsyn
Copy link

LayTsyn commented Feb 13, 2025

Go version

go1.23.4 windows/amd64

Output of go env in your module/workspace:

set GODEBUG=
set GOTELEMETRY=local
set GOTELEMETRYDIR=C:\Users\LayTsyn\AppData\Roaming\go\telemetry
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\LayTsyn\AppData\Local\Temp\go-build3606146757=/tmp/go-build -gno-record-gcc-switches

What did you do?

Program1:

package main

import (
	"fmt"
)

type EmptyCustomErr struct {
}

func (ce EmptyCustomErr) Error() string {
	return "EmptyCustomErr"
}

func ReturnMePointerEmpty() error {
	return &EmptyCustomErr{}
}

func main() {
	interfaceWithEmptyPointer := ReturnMePointerEmpty()
	pointerErrEmpty := &EmptyCustomErr{}
	fmt.Println("pointerErrEmpty == interfaceWithPointer: ", pointerErrEmpty == interfaceWithEmptyPointer)

	// fmt.Println(reflect.ValueOf(pointerErrEmpty))
}

Program2:

package main

import (
	"fmt"
	"reflect"
)

type EmptyCustomErr struct {
}

func (ce EmptyCustomErr) Error() string {
	return "EmptyCustomErr"
}

func ReturnMePointerEmpty() error {
	return &EmptyCustomErr{}
}

func main() {
	interfaceWithEmptyPointer := ReturnMePointerEmpty()
	pointerErrEmpty := &EmptyCustomErr{}
	fmt.Println("pointerErrEmpty == interfaceWithPointer: ", pointerErrEmpty == interfaceWithEmptyPointer)

	fmt.Println(reflect.ValueOf(pointerErrEmpty))
}

What did you see happen?

Program1

Output: "pointerErrEmpty == interfaceWithPointer: false"

Program2

Output: "pointerErrEmpty == interfaceWithPointer: true"

What did you expect to see?

I expected to see that regardless of calling the value acquisition method (reflect.ValueOf), the result of the comparison would be the same in the two types of program.

The Go documentation (https://pkg.go.dev/reflect#ValueOf) says:

func ValueOf
func ValueOf(i any) Value
ValueOf returns a new Value initialized to the concrete value stored in the interface i. ValueOf(nil) returns the zero Value.

There is no word here that the transmitted value is being changed. But that seems to be the case. Otherwise, why does calling the information Getter (ValueOf) change the result of the program execution?

@seankhliao
Copy link
Member

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Feb 13, 2025
@gabyhelp gabyhelp added the BugReport Issues describing a possible bug in the Go implementation. label Feb 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BugReport Issues describing a possible bug in the Go implementation.
Projects
None yet
Development

No branches or pull requests

3 participants