Go
1.5添加了对构建共享库的支持,这些共享库可从C(因此可以通过FFI从Ruby)调用。这使该过程比1.5版之前的版本(需要编写C胶合层)更容易,并且现在可以使用Go运行时,这在现实生活中实际上是有用的(以前无法进行goroutine和内存分配,因为它们需要Go运行时,如果Go不是主要入口点,则该运行时将无法使用)。
goFuncs.go:
package mainimport "C"//export GoAddfunc GoAdd(a, b C.int) C.int { return a + b}func main() {} // Required but ignored请注意,
//export GoAdd每个导出的功能都需要注释。之后的符号
export是函数的导出方式。
goFromRuby.rb:
require 'ffi'module GoFuncs extend FFI::Library ffi_lib './goFuncs.so' attach_function :GoAdd, [:int, :int], :intendputs GoFuncs.GoAdd(41, 1)
该库构建有:
go build -buildmode=c-shared -o goFuncs.so goFuncs.go
运行Ruby脚本会产生:
42



