Delta
Delta

Reputation: 157

Is there any way to build a source of go, including struct type, to a *.so file?

I'm constructing an application written by python.

In the application, I need a function provided by a go language, so I'm trying making a *.so file to use it as a local library.

How should I build the *.so with a go-lang source containing struct-type.


Go version : go version go1.12.2 windows/amd64

python : Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32


Up to now, I succeeded to build a *.so file with a go-lang source, just having a simple function without any struct type. And it worked by executing it via python-code.

Then, I added a struct parameters on the go-code, after that, tried a same build process. However, it never did work, showing some messages like this.

# command-line-arguments

.\user_auth.go:37:16: Go type not supported in export: http.ResponseWriter

.\user_auth.go:37:40: Go type not supported in export: http.Request

.\user_auth.go:37:16: Go type not supported in export: http.ResponseWriter

.\user_auth.go:37:40: Go type not supported in export: http.Request

According to here, cmd/cgo seems not to support this conversion till 2017. I could found no more infomation than above.

・Succeeded(ok.go)

package main

import (
    "C"
)


func main() {
}

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

・Failed(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)
}

I expect that go-lang code as above can be executed by python. Or, some other way to get user information in gcp(gae-py3.X) is welcome.

Upvotes: 5

Views: 1867

Answers (1)

Jordan C
Jordan C

Reputation: 127

The signature of the function that you export from Go to C must contain only C types (or primitive Go types that can be automatically converted to C types, like int). The documentation explicitly states that Go struct types are not supported; use a C struct type. (This is also discussed in the issue you linked.)

So that's why you can't use http.ResponseWriter or *http.Request in the signature of your exported function. You'll have to either define your own C types to represent an HTTP request and response (probably painful), or break up your local library differently (e.g. define separate loginURL and logoutURL functions that take and return strings).

Upvotes: 2

Related Questions