Skip to content

reflect: reflect.StructOf fields with Anonymous=true has not got Their func #59046

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
ssbeatty opened this issue Mar 15, 2023 · 2 comments
Closed
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge

Comments

@ssbeatty
Copy link

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

$ go version
go version go1.19.5 windows/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
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\wang\AppData\Local\go-build
set GOENV=C:\Users\wang\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\wang\go\pkg\mod
set GONOPROXY=*.gitlab.com,*.gitee.com
set GONOSUMDB=*.gitlab.com,*.gitee.com
set GOOS=windows
set CXX=g++
set CGO_ENABLED=1
set GOMOD=D:\PROJECTS\GO\gateway_test\go.mod
set GOWORK=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=C:\Users\wang\AppData\Local\Temp\go-build881389354=/tmp/go-build -gno-record-gcc-switches

What did you do?

I have a piece of code that has unexpected execution results
refer to the example in

func TestStructOfTooManyFields(t *testing.T) {

package main

import (
	"bytes"
	"reflect"
	"time"
)

func main() {
	tt := reflect.StructOf([]reflect.StructField{
		{Name: "Time", Type: reflect.TypeOf(time.Time{}), Anonymous: true},
	})

	if _, present := tt.MethodByName("After"); present {
		println("tt has After func")
	} else {
		println("tt has not After func")
	}

	bt := reflect.StructOf([]reflect.StructField{
		{Name: "Buffer", Type: reflect.TypeOf(bytes.Buffer{}), Anonymous: true},
	})

	if _, present := bt.MethodByName("Write"); present {
		println("bt has Write func")
	} else {
		println("bt has not Write func")
	}
}

What did you expect to see?

// output
// tt has After func
// bt has Write func

What did you see instead?

// output
// tt has After func
// bt has not Write func
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Mar 15, 2023
@shawnh2
Copy link

shawnh2 commented Mar 15, 2023

Here's the methods signature of Time.After and Buffer.Write:

func (b *Buffer) Write(p []byte) (n int, err error) {

func (t Time) After(u Time) bool {

You can see their receivers are not the same type, if you expect to see "bt has Write func", change type of Buffer to *Buffer:

bt := reflect.StructOf([]reflect.StructField{
	{Name: "Buffer", Type: reflect.PtrTo(reflect.TypeOf(bytes.Buffer{})), Anonymous: true},  // <=
})

@ssbeatty
Copy link
Author

Thanks

@golang golang locked and limited conversation to collaborators Mar 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge
Projects
None yet
Development

No branches or pull requests

3 participants