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

reflect: It does not print correct method set #67034

Closed
leabit opened this issue Apr 25, 2024 · 2 comments
Closed

reflect: It does not print correct method set #67034

leabit opened this issue Apr 25, 2024 · 2 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.

Comments

@leabit
Copy link

leabit commented Apr 25, 2024

Go version

go version go1.22.0 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/leabit/.cache/go-build'
GOENV='/home/leabit/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/leabit/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/leabit/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22.0'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='0'
GOMOD='/home/leabit/goprojects/ceogolang/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1848736934=/tmp/go-build -gno-record-gcc-switches'

What did you do?

I made a package mypackage with the following code:

package mypackage

import (
	"fmt"
	"reflect"
)

type Helper struct{}

func (Helper) Method1() {}
func (Helper) method2() { fmt.Println("From method2") }

func (h Helper) Print() {
	h.method2()

	for i := range reflect.TypeOf(h).NumMethod() {
		fmt.Println(reflect.TypeOf(h).Method(i))
	}
}

As you can see, the package is very simple. It only has one defined type (Helper) with 3 methods, 2 exported and one private (non-exported). The Print method is used to print method set of the Helper.

main.go is as follows:

package main

import (
	"ceogolang/mypackage"
	"fmt"
	"reflect"
)

func main() {
	h := mypackage.Helper{}

	for i := range reflect.TypeOf(h).NumMethod() {
		fmt.Println(reflect.TypeOf(h).Method(i))
	}

	h.Print()
}

The main function is also very simple, it just initializes the mypackage.Help struct variable and prints its method set (one directly in the main and one by invoking Print method).

What did you see happen?

The std output of the program is:

{Method1 func(mypackage.Helper) <func(mypackage.Helper) Value> 0}
{Print func(mypackage.Helper) <func(mypackage.Helper) Value> 1}
From method2
{Method1 func(mypackage.Helper) <func(mypackage.Helper) Value> 0}
{Print func(mypackage.Helper) <func(mypackage.Helper) Value> 1}

What did you expect to see?

I expect to see method2 in the second print as well, since (looking "directly" at the mypackage) the helper method set also includes method2 (I invoked it in the Print method).

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Apr 25, 2024
@nussjustin
Copy link
Contributor

This is working as intended.

From the documentation for Type.Method:

// For a non-interface type T or *T, the returned Method's Type and Func
// fields describe a function whose first argument is the receiver,
// and only exported methods are accessible.

Similary for Type.NumMethod

// For a non-interface type, it returns the number of exported methods.

method2 is not exported, so it is not returned by Method nor counted by NumMethod.

@leabit
Copy link
Author

leabit commented Apr 25, 2024

@nussjustin Thanks!

@leabit leabit closed this as completed Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.
Projects
None yet
Development

No branches or pull requests

3 participants