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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...

When using sql.Open, why doesn’t the DSN report an error? In Go language, sql.Open...
