What is the correct way to convert Go func to uintptr?
In the Go language, converting a `func` type to a `uintptr` type is a common operation, but the correct method is not to convert directly, because this may leading to some potential problems. The correct method is to use the `Pointer` function in the `unsafe` package to convert the `func` type value to the `unsafe.Pointer` type first, and then convert it to the `uintptr` type. This ensures the safety of type conversion and avoids unpredictable errors. Although this method requires the use of the `unsafe` package, under the right circumstances, it can effectively solve the problem of converting `Go func` to `uintptr`.
Question content
I need to pass and receive go functions in go code.
Due to the way system calls work in the go language, the type used for "passage" is uintptr
.
I have no choice but uintptr
since syscall.syscalln
accepts and returns this type.
What is the correct way to convert go func
to uintptr
?
I tried using it in the sandbox but I can't simply convert it.
package main import ( "fmt" "unsafe" ) func main() { var f MyFunc = SumInt fmt.Println(f(1, 2)) test(uintptr(f)) // Cannot convert an expression of the type 'MyFunc' to the type 'uintptr' test(uintptr(unsafe.Pointer(f))) // Cannot convert an expression of the type 'MyFunc' to the type 'unsafe.Pointer' } type MyFunc func(a int, b int) (sum int) func SumInt(a, b int) int { return a + b } func test(x uintptr) { var xf MyFunc xf = MyFunc(x) // Cannot convert an expression of the type 'uintptr' to the type 'MyFunc' xf = MyFunc(unsafe.Pointer(x)) // Cannot convert an expression of the type 'unsafe.Pointer' to the type 'MyFunc' fmt.Println(xf(1, 2)) }
I searched on the internet but this information cannot be seen directly in google.
Thanks.
Solution
I found a solution! I need to pass a function pointer.
package main import ( "fmt" "unsafe" ) func main() { var f myfunc = sumint fmt.println(f(1, 2)) test(uintptr(unsafe.pointer(&f))) } type myfunc func(a int, b int) (sum int) func sumint(a, b int) int { return a + b } func test(x uintptr) { var xfp *myfunc xfp = (*myfunc)(unsafe.pointer(x)) var xf myfunc xf = *xfp fmt.println(xf(1, 2)) }
3 3 Process finished with the exit code 0
The above is the detailed content of What is the correct way to convert Go func to uintptr?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use reflection to access private fields and methods in golang

Tips for dynamically creating new functions in golang functions

The difference between performance testing and unit testing in Go language

What pitfalls should we pay attention to when designing distributed systems with Golang technology?

Golang technology libraries and tools used in machine learning

The role of Golang technology in mobile IoT development

The evolution of golang function naming convention

Can golang variable parameters be used for function return values?
