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

Struct Reflection result is wrong #41667

Closed
CengSin opened this issue Sep 28, 2020 · 2 comments
Closed

Struct Reflection result is wrong #41667

CengSin opened this issue Sep 28, 2020 · 2 comments

Comments

@CengSin
Copy link

@CengSin CengSin commented Sep 28, 2020

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

$ go version
go version go1.15.2 darwin/amd64

Does this issue reproduce with the latest release?

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

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/cengsin/Library/Caches/go-build"
GOENV="/Users/cengsin/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/cengsin/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/cengsin/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/cengsin/GolandWS/GoModuleDemo/go.mod"
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/dt/4jzmc_m167s8cy0b5pw0qgmm0000gn/T/go-build998003084=/tmp/go-build -gno-record-gcc-switches -fno-common"
GOROOT/bin/go version: go version go1.15.2 darwin/amd64
GOROOT/bin/go tool compile -V: compile version go1.15.2
uname -v: Darwin Kernel Version 19.6.0: Mon Aug 31 22:12:52 PDT 2020; root:xnu-6153.141.2~1/RELEASE_X86_64
ProductName:	Mac OS X
ProductVersion:	10.15.7
BuildVersion:	19H2
lldb --version: lldb-1103.0.22.10
Apple Swift version 5.2.4 (swiftlang-1103.0.32.9 clang-1103.0.32.53)

What did you do?

type A2 struct {
	Name string
}

func main() {
	Func1(&A2{})
}

func Func1(values ...interface{}) {
	for _, value := range values {
		Func2(value)
	}
}

func Func2(values ...interface{}) {
	Func3(values)
}

func Func3(values ...interface{}) {
	Func4(values)
}

func Func4(values interface{}) {
	modelType := reflect.TypeOf(values)
	for modelType.Kind() == reflect.Slice || modelType.Kind() == reflect.Array || modelType.Kind() == reflect.Ptr {
		modelType = modelType.Elem()
	}
	fmt.Println(modelType.Kind())
}

output:

/private/var/folders/dt/4jzmc_m167s8cy0b5pw0qgmm0000gn/T/___go_build_GoModuleDemo_go__2_
interface

Should be a structure type, the result becomes an interface

What did you expect to see?

result is Struct

What did you see instead?

type A2 struct {
	Name string
}

func main() {
	Func1(&A2{})
}

func Func1(values ...interface{}) {
	for _, value := range values {
		Func2(value)
	}
}

func Func2(values ...interface{}) {
	for _, value := range values {
		Func3(value)
	}
}

func Func3(values ...interface{}) {
	for _, value := range values {
		Func4(value)
	}
}

func Func4(values interface{}) {
	modelType := reflect.TypeOf(values)
	for modelType.Kind() == reflect.Slice || modelType.Kind() == reflect.Array || modelType.Kind() == reflect.Ptr {
		modelType = modelType.Elem()
	}
	fmt.Println(modelType.Kind())
}

This way is the correct result

@D1CED
Copy link

@D1CED D1CED commented Sep 28, 2020

What gets passed to Func4 is different in both versions. If we add the statement fmt.Printf("%#v\n", values) this becomes apparent.

The first version produces []interface {}{[]interface {}{(*main.A2)(0xc000010060)}} (arbitrary pointer value) while the second version outputs &main.A2{Name:""}.

@mvdan
Copy link
Member

@mvdan mvdan commented Sep 28, 2020

The Go project doesn't use the issue tracker for questions. See https://golang.org/wiki/Questions.

@mvdan mvdan closed this Sep 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.