有没有办法为* .so文件构建go的源代码,包括struct类型?

问题描述 投票:3回答:1

我正在构建一个由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-code执行它。

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

#command-line-arguments

。\ 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)获取用户信息的其他方法。

python go hybrid
1个回答
0
投票

从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函数,它们接收和返回字符串)。

© www.soinside.com 2019 - 2024. All rights reserved.