ElectronアプリからGoのコードを利用

Kaoru Mori
Oct 2, 2018 · 4 min read

Python

$ pyenv local 2.7.15

node-ffi

$ npm install node-ffi/node-ffi

共有ライブラリの作成

const ffi = require("ffi");
const mylib = ffi.Library('libtest', {
'strprint': ['void', ['string']]
});
mylib.strprint("Hello,go! from Node.js");
package mainimport (
"C"
"fmt"
)

//export strprint
func strprint(cstr *C.char) {
fmt.printf(C.GoString(cstr)
}

func main() {
}
# macOS
$ go build -buildmode=c-shared -o libtest.dylib test.g

実行

$ (npm bin)/electron-rebuild
$ npm start

electron-builderの利用

"build": {
"extraResources": "lib",
}
const path = require("path");const libPath = process.env.NODE_ENV === 'development'
? path.join(__dirname, 'lib', 'libname')
: path.join(process.resourcesPath, 'lib', 'libname')
Kaoru Mori

Written by

ねこぱんち研究所 主任研究員, kaoru.mori@gmail.com

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade