go-interlang

Examples of calls between Go and C/C++ (and how to call a Go shared object from Node/Ruby/Python/Java)

  • 所有者: draffensperger/go-interlang
  • 平台:
  • 許可證:
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

Go interlanguage call examples

Examples of calls between Go and C/C++ and calling Go from dynamic languages.

The examples are designed to work on Mac and Linux. If you're using Windows,
feel free to reach out to me to request updates for examples that don't work
correctly.

Calls from Go to C (go_to_c folder)

These examples use cgo to enable calls to C.

  • Calling a C snippet in the cgo comment: c_in_comment
  • Calling a C statically-linked library (.a file): static_c_lib
  • Calling a C dynamically-linked library (.so or .dylib): dynamic_c_lib

Calls from Go to C++ (go_to_cxx folder)

C++ has more complex calling conventions (e.g. function overloading, inheritance, templates) and so it uses name mangling which adds a step when calling it from Go. Below are ways to do it.

  • Calling C++ via a C wrapper function: c_wrapper
  • Calling C++ via an auto-generated SWIG wrapper:
    swig

Calls from C/C++ to Go (c_to_go folder)

Calls from Python/Node.js/Ruby/Java to Go (dyn_langs_to_go folder)

Go now allows building a C-compatible dynamically-linked library with buildmode=c-shared. That allows any language that can call C dynamic libraries to call Go.

Cross-language call benchmarks

There is a cost to calling between languages a Go to C call is about 50x slower
than a pure Go call and a Ruby FFI call is about 30x slower than a pure Ruby
call. For more details, see the benchmarks section.

Helpful Links

Cgo documentation: golang.og/cmd/cgo/

Documentation for go command (see especially go build section and "Calling between Go and C"):
golang.org/cmd/go/

SWIG Documentation: swig.org

SWIG Go examples: github.com/swig/swig/tree/master/Examples/go

Gccgo documentation: golang.org/doc/install/gccgo

License

The code in this repo is MIT Licensed.

主要指標

概覽
名稱與所有者draffensperger/go-interlang
主編程語言Go
編程語言C (語言數: 9)
平台
許可證
所有者活动
創建於2015-10-11 01:20:42
推送於2017-04-14 01:13:13
最后一次提交2017-04-13 21:13:13
發布數0
用户参与
星數430
關注者數15
派生數63
提交數44
已啟用問題?
問題數6
打開的問題數2
拉請求數1
打開的拉請求數0
關閉的拉請求數0
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?