Skip to content

Instantly share code, notes, and snippets.

@hatajoe
Last active October 6, 2015 15:52
Show Gist options
  • Save hatajoe/47ddd2fd18906689de7d to your computer and use it in GitHub Desktop.
Save hatajoe/47ddd2fd18906689de7d to your computer and use it in GitHub Desktop.
This is not work for error "Go type not supported in export: "
// file: main.go
package main
import "C"
import (
"bitbucket.org/binet/go-ffi/pkg/ffi"
"fmt"
"net/http"
)
func main() {
lib, err := ffi.NewLibrary("./libs/profile.so")
if err != nil {
panic(err)
}
defer lib.Close()
// I guess I am wrong type specified, but I have no idea for this line...
fn, err := lib.Fct("Profile", ffi.Void, []ffi.Type{ffi.Void})
if err != nil {
panic(err)
}
// execute exported function
h := fn(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, World")
})
http.HandleFunc("/", h.Interface().(http.HandlerFunc))
http.ListenAndServe(":8080", nil)
}
// file: plugins/profile/profile.go
// go build -buildmode=c-shared -o ./lib/profile.so ./plugins/profile
package main
import (
"C"
"log"
"net/http"
"time"
)
//export Profile
func Profile(fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
b := time.Now()
defer log.Printf("elapsed: %v\n", time.Now().Sub(b))
fn(w, r)
}
}
func init() {
log.Println("loaded")
}
func main() {
}
@hatajoe
Copy link
Author

hatajoe commented Oct 6, 2015

here's my environment

[vagrant@localhost go-plugin-example]$ uname -a
Linux localhost.localdomain 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
[vagrant@localhost go-plugin-example]$ go version
go version go1.5.1 linux/amd64
[vagrant@localhost go-plugin-example]$ tree
.
|-- libs
|   |-- profile.h
|   `-- profile.so
|-- main.go
`-- plugins
    `-- profile
        `-- main.go

3 directories, 4 files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment