Wednesday, March 22, 2023
HomeGolangExactly how to obtain the real address of the approach? - Obtaining...

Exactly how to obtain the real address of the approach? – Obtaining Aid


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

 bundle primary

import (
" fmt".
" show".
).

kind MyType struct {}

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

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

myMethod:= myType.Method( 0 ).

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

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

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

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

Making use of the “show” typical bundle I acquired depiction of MyMethod as well as put it in the myMethod variable. Printing that variable returns the following (significant with the “very first 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 trying to find, yet when I go into the meaning of reflect.Value, remark for ptr is:

// Pointer-valued information or, if flagIndir is established, tip 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 looking) just if the flag flagIndir is established. So my below concern would certainly be just how to see if it’s established or otherwise? Likewise, what does “pointer-valued information” indicate?

Given that I could not discover whether the provided flag was established or otherwise, I attempted one more method to get to the objective. The following point I attempted was to publish the address of the approach straight (noted with the “2nd print” remark). The print outcome entirely puzzled me as it was entirely various from the one with ptr (0xc00000a048). The print generated the list below outcome: 0xcacf80

Lastly, I additionally attempted to obtain the ptr straight as well as I did it (a minimum of I assume I did) as well as placed the worth in the myPointer variable. Publishing the myPointer worth just boosted 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 discovered this:

If v’s Kind is Func, the returned tip is a hidden code tip, yet not always adequate to determine a solitary feature distinctively. The only assurance is that the outcome is absolutely 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 approach? So, another below concern is: Does it indicate that feature Tip() does not return ptr yet another thing?

Would certainly behave if a person of you additionally respond to on below concern, yet my primary as well as crucial concern is: Exactly how to obtain the real (” actual”) address of the approach?

RELATED ARTICLES

Most Popular

Recent Comments