Table of Contents
Question content
Solution
Home Backend Development Golang What is the correct way to convert Go func to uintptr?

What is the correct way to convert Go func to uintptr?

Feb 09, 2024 pm 01:10 PM
go language

将 Go func 转换为 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))
}
Copy after login

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))
}
Copy after login
3
3

Process finished with the exit code 0
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use reflection to access private fields and methods in golang How to use reflection to access private fields and methods in golang May 03, 2024 pm 12:15 PM

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

Tips for dynamically creating new functions in golang functions Tips for dynamically creating new functions in golang functions Apr 25, 2024 pm 02:39 PM

Tips for dynamically creating new functions in golang functions

The difference between performance testing and unit testing in Go language The difference between performance testing and unit testing in Go language May 08, 2024 pm 03:09 PM

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? What pitfalls should we pay attention to when designing distributed systems with Golang technology? May 07, 2024 pm 12:39 PM

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

Golang technology libraries and tools used in machine learning Golang technology libraries and tools used in machine learning May 08, 2024 pm 09:42 PM

Golang technology libraries and tools used in machine learning

The role of Golang technology in mobile IoT development The role of Golang technology in mobile IoT development May 09, 2024 pm 03:51 PM

The role of Golang technology in mobile IoT development

The evolution of golang function naming convention The evolution of golang function naming convention May 01, 2024 pm 03:24 PM

The evolution of golang function naming convention

Can golang variable parameters be used for function return values? Can golang variable parameters be used for function return values? Apr 29, 2024 am 11:33 AM

Can golang variable parameters be used for function return values?

See all articles