Closed shen-xianpeng closed 4 years ago
Is this a question or are you telling me how to do it ? Please clarify.
I'm actually having trouble just calling with args that are string arrays:
my go code:
// +build lib
package main
import "C"
import (
"github.com/sahilm/fuzzy"
)
func main() {}
//export FindMatches
func FindMatches(data []string, pattern string, max int) []string {
matches := fuzzy.Find(pattern, data)
if max == 0 {
max = len(matches)
}
max = min(len(matches), max)
arr := make([]string, max)
// log.Debugf("number of matches: %v", len(matches))
// log.Infof("[%v] Size=%v, qry=%v, matches=%v, max=%v", cid, len(data), pattern, len(matches), max)
for i, m := range matches[:max] {
arr[i] = m.Str
// log.Debug(m.Str)
}
return arr
}
func min(x, y int) int {
if x < y {
return x
}
return y
}
the header file generated has this definition:
extern GoSlice FindMatches(GoSlice p0, GoString p1, GoInt p2);
I'd like to call this from python but not sure how to proceed.
Check out how the GoSlice
type maps to a Python
object in the write up https://github.com/vladimirvivien/go-cshared-examples#from-python
<snip>
# define class GoSlice to map to:
# C type struct { void *data; GoInt len; GoInt cap; }
class GoSlice(Structure):
_fields_ = [("data", POINTER(c_void_p)), ("len", c_longlong), ("cap", c_longlong)]
</snip>
Then create the slice in python
nums = GoSlice((c_void_p * 5)(74, 4, 122, 9, 12), 5, 5)
Also, the following shows how to map GoSlice type to python struct using CFFI library - https://github.com/vladimirvivien/go-cshared-examples#python-cffi-contributed
Hope that helped!
After reading both links (and many others ) I didn't find solution how to pass array of strings to go library from python. All examples shows how to work with array of ints only. Please, can anybody provide a piece of code to describe how to create GoSlice from array of strings in python so it can be passed to func Sort(vals []string) in go ?
None of these examples show how to expose any non-primitive type (struct, slice etc) to calling code in another language (and it isn't straightforward).
The example shows how to pass a slice of integers in many of the languages.
i want to call function ` func ArrayArgs(data [][]string) []string { return []string{"123"} }
`