You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
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).
The text was updated successfully, but these errors were encountered:
// 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.
Go version
go version go1.22.0 linux/amd64
Output of
go env
in your module/workspace:What did you do?
I made a package
mypackage
with the following code: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). ThePrint
method is used to print method set of theHelper
.main.go
is as follows: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 invokingPrint
method).What did you see happen?
The std output of the program is:
What did you expect to see?
I expect to see
method2
in the second print as well, since (looking "directly" at themypackage
) the helper method set also includesmethod2
(I invoked it in thePrint
method).The text was updated successfully, but these errors were encountered: