{"id":40581718,"url":"https://github.com/draffensperger/go-interlang","last_synced_at":"2026-01-21T02:36:16.726Z","repository":{"id":50427582,"uuid":"44034668","full_name":"draffensperger/go-interlang","owner":"draffensperger","description":"Examples of calls between Go and C/C++ (and how to call a Go shared object from Node/Ruby/Python/Java)","archived":false,"fork":false,"pushed_at":"2017-04-14T01:13:13.000Z","size":2438,"stargazers_count":430,"open_issues_count":2,"forks_count":63,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-08-14T11:59:20.622Z","etag":null,"topics":["cgo","ffi","golang","swig"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/draffensperger.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-10-11T01:20:42.000Z","updated_at":"2025-06-19T07:58:46.000Z","dependencies_parsed_at":"2022-08-31T23:02:28.581Z","dependency_job_id":null,"html_url":"https://github.com/draffensperger/go-interlang","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/draffensperger/go-interlang","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/draffensperger%2Fgo-interlang","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/draffensperger%2Fgo-interlang/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/draffensperger%2Fgo-interlang/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/draffensperger%2Fgo-interlang/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/draffensperger","download_url":"https://codeload.github.com/draffensperger/go-interlang/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/draffensperger%2Fgo-interlang/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28623467,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-20T23:49:58.628Z","status":"online","status_checked_at":"2026-01-21T02:00:08.227Z","response_time":86,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cgo","ffi","golang","swig"],"created_at":"2026-01-21T02:36:15.595Z","updated_at":"2026-01-21T02:36:16.717Z","avatar_url":"https://github.com/draffensperger.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Go interlanguage call examples\n\nExamples of calls between Go and C/C++ and calling Go from dynamic languages.\n\nThe examples are designed to work on Mac and Linux. If you're using Windows,\nfeel free to reach out to me to request updates for examples that don't work\ncorrectly.\n\n## Calls from Go to C (`go_to_c` folder)\n\nThese examples use [cgo](https://golang.org/cmd/cgo/) to enable calls to C.\n\n- Calling a C snippet in the cgo comment: [c_in_comment](https://github.com/draffensperger/go-interlang/tree/master/go_to_c/c_in_comment/main.go)\n- Calling a C statically-linked library (`.a` file): [static_c_lib](https://github.com/draffensperger/go-interlang/tree/master/go_to_c/static_c_lib)\n- Calling a C dynamically-linked library (`.so` or `.dylib`): [dynamic_c_lib](https://github.com/draffensperger/go-interlang/tree/master/go_to_c/dynamic_c_lib)\n\n## Calls from Go to C++ (`go_to_cxx` folder)\n\nC++ has more complex calling conventions (e.g. function overloading, inheritance, templates) and so it uses [name mangling](https://en.wikipedia.org/wiki/Name_mangling#Name_mangling_in_C.2B.2B) which adds a step when calling it from Go. Below are ways to do it.\n\n- Calling C++ via a C wrapper function: [c_wrapper](https://github.com/draffensperger/go-interlang/tree/master/go_to_cxx/c_wrapper)\n- Calling C++ via an auto-generated SWIG wrapper:\n  [swig](https://github.com/draffensperger/go-interlang/tree/master/go_to_cxx/swig)\n\n## Calls from C/C++ to Go (`c_to_go` folder)\n\n- Calling from Go to C and back again: [to_c_and_back](https://github.com/draffensperger/go-interlang/tree/master/go_from_c/to_c_and_back)\n- Passing a Go func as function pointer callback to C: [callbacks](https://github.com/draffensperger/go-interlang/tree/master/go_from_c/callbacks)\n- Calling a Go static library with `buildmode=c-archive` from C: [static_go_lib](https://github.com/draffensperger/go-interlang/tree/master/c_to_go/static_go_lib)\n- Calling a Go dynamic lib from C++ [cxx_to_go_dyn_lib](https://github.com/draffensperger/go-interlang/tree/master/c_to_go/cxx_to_go_dyn_lib)\n- Calling Go from C using [gccgo](https://golang.org/doc/install/gccgo)\n\n## Calls from Python/Node.js/Ruby/Java to Go (`dyn_langs_to_go` folder)\n\nGo 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.\n\n- Call from Python via [ctypes](https://docs.python.org/2/library/ctypes.html):\n  [python_to_go.py](https://github.com/draffensperger/go-interlang/blob/master/dyn_langs_to_go/python_to_go.py)\n- Call from Node.js via [ffi](https://github.com/node-ffi/node-ffi) npm module:\n  [nodejs_to_go.js](https://github.com/draffensperger/go-interlang/blob/master/dyn_langs_to_go/nodejs_to_go.js)\n- Call from Ruby via [ffi](https://github.com/ffi/ffi) gem: [ruby_to_go.rb](https://github.com/draffensperger/go-interlang/blob/master/dyn_langs_to_go/ruby_to_go.rb)\n- Call from Java via [JNA](https://github.com/java-native-access/jna): [JavaToGo.java](https://github.com/draffensperger/go-interlang/blob/master/dyn_langs_to_go/java_to_go/src/main/java/javatogo/JavaToGo.java)\n\n# Cross-language call benchmarks\n\nThere is a cost to calling between languages a Go to C call is about 50x slower\nthan a pure Go call and a Ruby FFI call is about 30x slower than a pure Ruby\ncall. For more details, see the [benchmarks](https://github.com/draffensperger/go-interlang/tree/master/benchmarks) section.\n\n# Helpful Links\n\nCgo documentation: [golang.og/cmd/cgo/](https://golang.org/cmd/cgo/)\n\nDocumentation for `go` command (see especially `go build` section and \"Calling between Go and C\"): \n[golang.org/cmd/go/](https://golang.org/cmd/go/)\n\nSWIG Documentation: [swig.org](http://swig.org/)\n\nSWIG Go examples: [github.com/swig/swig/tree/master/Examples/go](https://github.com/swig/swig/tree/master/Examples/go)\n\nGccgo documentation: [golang.org/doc/install/gccgo](https://golang.org/doc/install/gccgo)\n\n# License\n\nThe code in this repo is [MIT Licensed](http://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdraffensperger%2Fgo-interlang","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdraffensperger%2Fgo-interlang","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdraffensperger%2Fgo-interlang/lists"}