【问题标题】:Is there any way to build a source of go, including struct type, to a *.so file?有什么方法可以将 go 的源代码(包括结构类型)构建到 *.so 文件中?
【发布时间】:2019-04-18 10:45:43
【问题描述】:

我正在构建一个用python编写的应用程序。

在应用程序中, 我需要一个 go 语言提供的函数,所以我正在尝试制作一个 *.so 文件以将其用作本地库。

我应该如何使用包含 struct-type 的 go-lang 源构建 *.so。


Go 版本:go 版本 go1.12.2 windows/amd64

python:win32 上的 Python 3.6.1(v3.6.1:69c0db5,2017 年 3 月 21 日,18:41:36)[MSC v.1900 64 位(AMD64)]


到现在为止,我成功地用 go-lang 源码构建了一个 *.so 文件,只是有一个没有任何结构类型的简单函数。它通过python代码执行它来工作。

然后,我在 go-code 上添加了一个结构参数,然后尝试了相同的构建过程。但是,它从来没有起作用,显示了一些这样的消息。

#命令行参数

.\user_auth.go:37:16:导出中不支持 Go 类型:http.ResponseWriter

.\user_auth.go:37:40:导出中不支持 Go 类型:http.Request

.\user_auth.go:37:16:导出中不支持 Go 类型:http.ResponseWriter

.\user_auth.go:37:40:导出中不支持 Go 类型:http.Request

根据here,cmd/cgo 似乎要到 2017 年才支持这种转换。 我找不到比上面更多的信息了。

·成功(ok.go)

package main

import (
    "C"
)


func main() {
}

//export adder
func adder(a, b int) int {
    return a + b
}

·失败(wish_ok.go)

package main

import (
        "fmt"
        "net/http"
        "C"

        "google.golang.org/appengine"
        "google.golang.org/appengine/user"
)

func main() {
//  http.HandleFunc("/auth", welcome)
//
//  appengine.Main()
}

func init() {
//    log.Println("Loaded!!")
}

//export welcome
func welcome(w http.ResponseWriter, r *http.Request) {
        ctx := appengine.NewContext(r)
        u := user.Current(ctx)
        if u == nil {
                url, _ := user.LoginURL(ctx, "/")
                fmt.Fprintf(w, `<a href="%s">Sign in or register</a>`, url)
                return
        }
        url, _ := user.LogoutURL(ctx, "/")
        fmt.Fprintf(w, `Welcome, %s! (<a href="%s">sign out</a>)`, u, url)
}

我希望上面的 go-lang 代码可以被 python 执行。 或者,欢迎通过其他方式在 gcp(gae-py3.X) 中获取用户信息。

【问题讨论】:

  • 目前go plugins (.so)文件只支持linux,你还需要导出你想在别处使用的函数/类型

标签: python go hybrid


【解决方案1】:

从 Go 导出到 C 的函数的签名必须仅包含 C 类型(或可以自动转换为 C 类型的原始 Go 类型,例如 int)。文档明确指出Go struct types are not supported; use a C struct type。 (这也是您链接的问题中的discussed。)

这就是为什么您不能在导出函数的签名中使用http.ResponseWriter*http.Request。您必须定义自己的 C 类型来表示 HTTP 请求和响应(可能很痛苦),或者以不同的方式分解本地库(例如,定义单独的 loginURLlogoutURL 接受和返回字符串的函数)。

【讨论】:

    猜你喜欢
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    • 2013-11-10
    • 2010-09-09
    • 1970-01-01
    • 1970-01-01
    • 2018-05-12
    • 2022-08-18
    相关资源
    最近更新 更多