我正在尝试导出一些Go函数,并使用南国防军在Java中调用它们,但我不知道如何在Java中为具有多个返回值的Go函数定义接口。
假设Go函数是:
//export generateKeys
func generateKeys() (privateKey, publicKey []byte) {
return .....
}返回值有两个项,但在Java中,只允许一个返回值。
我能做什么?
发布于 2018-04-21 15:19:23
cgo为多个返回值创建一个专用的C结构,单个返回值作为struct元素。
在您的示例中,cgo将生成
/* Return type for generateKeys */
struct generateKeys_return {
GoSlice r0; /* privateKey */
GoSlice r1; /* publicKey */
};函数将有一个不同的签名,然后通过JNA使用。
extern struct generateKeys_return generateKeys();在JNA定义中,您将使用JNA概念(未测试代码)类似于结构:
public interface Generator extends Library {
public class GoSlice extends Structure {
public Pointer data;
public long len;
public long cap;
protected List getFieldOrder(){
return Arrays.asList(new String[]{"data","len","cap"});
}
}
public class GenerateKeysResult extends Structure {
public GoSlice privateKey;
public GoSlice publicKey;
protected List getFieldOrder(){
return Arrays.asList(new String[]{"privateKey","publicKey"});
}
}
public GenerateKeysResult generateKeys();
}https://stackoverflow.com/questions/49956986
复制相似问题