Sunday, March 12, 2023
HomeGolangJust how to obtain the real address of the technique? - Obtaining...

Just how to obtain the real address of the technique? – Obtaining Assistance

I am looking for the real address of the technique MyMethod of kind MyType, yet I faced a tiny (huge) issue.

 plan major

import (
" fmt".
" mirror".

kind MyType struct {}

func (MyType) MyMethod( string) int {
return 0.

func major() {
myType:= reflect.TypeOf( MyType {} ).

myMethod:= myType.Method( 0 ).

fmt.Printf("% #vn", myMethod)// initial print.

fmt.Printf("% pn", MyType.MyMethod)// 2nd print.

myPointer:= myType.Method( 0 ). Func.Pointer().

fmt.Printf("% #vn", myPointer)// 3rd print.

Making use of the “mirror” typical plan I got depiction of MyMethod as well as put it in the myMethod variable. Printing that variable returns the following (significant with the “initial print” remark):

reflect.Method {Call:" MyMethod", PkgPath:"", Kind: (* reflect.rtype)( 0xcbc240), Func: reflect.Value {typ: (* reflect.rtype)( 0xcbc240), ptr:( unsafe.Pointer)( 0xc00000a048), flag:0 x13}, Index:0}

It appears like ptr has an address I’m seeking, yet when I get in the meaning of reflect.Value, remark for ptr is:

// Pointer-valued information or, if flagIndir is established, guideline to information.
// Legitimate when either flagIndir is established or typ.pointers() holds true.

It appears like the address 0xc00000a048 stands (is the outcome of my browsing) just if the flag flagIndir is established. So my below concern would certainly be exactly how to see if it’s established or otherwise? Additionally, what does “pointer-valued information” suggest?

Given that I could not locate whether the offered flag was established or otherwise, I attempted an additional means to get to the objective. The following point I attempted was to publish the address of the technique straight (noted with the “2nd print” remark). The print outcome totally puzzled me as it was totally various from the one with ptr (0xc00000a048). The print generated the list below outcome: 0xcacf80

Ultimately, I likewise attempted to obtain the ptr straight as well as I did it (at the very least I assume I did) as well as placed the worth in the myPointer variable. Publishing the myPointer worth just enhanced my complication (noted with the “3rd print” remark). The published worth was once more various from the one from ptr (0xc00000a048) yet coincided as the one from the 2nd hard copy. So I obtained the following: 0xcacf80

By checking out the remark pertaining to Func.Pointer() I located this:

If v’s Kind is Func, the returned guideline is a hidden code guideline, yet not always sufficient to recognize a solitary feature distinctly. The only assurance is that the outcome is no if as well as just if v is a nil func Worth.

It appears like this feature does not have anything with the ptr as well as the address of the technique? So, another below concern is: Does it suggest that feature Guideline() does not return ptr yet another thing?

Would certainly behave if somebody of you likewise respond to on below concern, yet my major as well as crucial concern is: Just how to obtain the real (” genuine”) address of the technique?


Most Popular

Recent Comments