概述
在 Java 中调用 Go 的大致过程如下go --> cgo --> jna --> java
整个过程要解决的问题主要两个:数据类型在两种语言中如何转化
何时清理无用的数据
下面就围绕上述调用过程来阐述,本文涉及代码完整版可以下面链接找到:
Go -> Cgo
这是跨语言调用的第一步,主要是借助 cgo,把 Go 代码编译 C 共享库。
cgo 是 Go 语言提供与 C 语言互调的一工具。提供一个名为 C 的伪 package,供 Go 访问 C 中的变量与函数,如 C.size_t C.stdout 等;同时提供 5 个特殊函数,用于两种语言间类型的转化:// Go string to C string
// The C string is allocated in the C heap using malloc.
// It is the caller's responsibility to arrange for it to be
// freed, such as by calling C.free (be sure to include stdlib.h
// if C.free is needed).
func C.CString(string) *C.char
// Go []byte slice to C array
// The C array is allocated in the C heap using malloc.
// It is the caller's responsibility to arrange for it to be
// freed, such as by calling C.free (be sure
最后
以上就是冷静小懒虫为你收集整理的java 调用 go_实践总结:在 Java 中调用 Go 代码的全部内容,希望文章能够帮你解决java 调用 go_实践总结:在 Java 中调用 Go 代码所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复