{"id":19998101,"url":"https://github.com/wenlng/gonacli","last_synced_at":"2025-10-07T23:21:15.219Z","repository":{"id":64646197,"uuid":"576935617","full_name":"wenlng/gonacli","owner":"wenlng","description":"🖖 This is a development tool that can quickly use Golang to develop and build NodeJS Addon.","archived":false,"fork":false,"pushed_at":"2024-11-23T07:20:02.000Z","size":1247,"stargazers_count":43,"open_issues_count":1,"forks_count":9,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-04T14:46:53.837Z","etag":null,"topics":["addon","c","cpp","devtools","go","golang","javascript","js","napi","nodejs","nodejsaddon","v8"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wenlng.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-12-11T13:23:00.000Z","updated_at":"2025-04-03T15:59:59.000Z","dependencies_parsed_at":"2025-05-04T14:44:26.390Z","dependency_job_id":null,"html_url":"https://github.com/wenlng/gonacli","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/wenlng/gonacli","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wenlng%2Fgonacli","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wenlng%2Fgonacli/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wenlng%2Fgonacli/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wenlng%2Fgonacli/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wenlng","download_url":"https://codeload.github.com/wenlng/gonacli/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wenlng%2Fgonacli/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266117735,"owners_count":23879109,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["addon","c","cpp","devtools","go","golang","javascript","js","napi","nodejs","nodejsaddon","v8"],"created_at":"2024-11-13T05:07:21.967Z","updated_at":"2025-10-07T23:21:10.178Z","avatar_url":"https://github.com/wenlng.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\u003cimg width=\"120\" style=\"padding-top: 50px\" src=\"http://47.104.180.148/gonacli/gonacli_logo.svg\"/\u003e\n\u003ch1 style=\"margin: 0; padding: 0\"\u003eGonaCli\u003c/h1\u003e\n\u003cp\u003eThis is a development tool that can quickly use Golang to develop NodeJS Addon.\u003c/p\u003e\n\u003ca href=\"https://goreportcard.com/report/github.com/wenlng/gonacli\"\u003e\u003cimg src=\"https://goreportcard.com/badge/github.com/wenlng/gonacli\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://godoc.org/github.com/wenlng/gonacli\"\u003e\u003cimg src=\"https://godoc.org/github.com/wenlng/gonacli?status.svg\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/wenlng/gonacli/releases\"\u003e\u003cimg src=\"https://img.shields.io/github/v/release/wenlng/gonacli.svg\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/wenlng/gonacli/blob/master/LICENSE\"\u003e\u003cimg src=\"https://img.shields.io/github/license/wenlng/gonacli.svg\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/wenlng/gonacli\"\u003e\u003cimg src=\"https://img.shields.io/github/stars/wenlng/gonacli.svg\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/wenlng/gonacli\"\u003e\u003cimg src=\"https://img.shields.io/github/last-commit/wenlng/gonacli.svg\"/\u003e\u003c/a\u003e\n\u003c/div\u003e\n\n\u003cbr/\u003e\n\n\u003e English | [中文](README_zh.md)\n\n\n\u003ca href=\"https://github.com/wenlng/gonacli\"\u003eGONACLI\u003c/a\u003e is a development tool that quickly uses Golang to develop \u003cb\u003eNodeJS Addon\u003c/b\u003e. You only need to concentrate on the development of Golang, and you don't need to care about the implementation of the bridge layer. It supports JavaScript sync calls and async callbacks.\n\n\u003cbr/\u003e\n\n ⭐️ If it helps you, please give a star.\n\n- [https://github.com/wenlng/gonacli](https://github.com/wenlng/gonacli)\n\n\n\u003cbr/\u003e\n\n\n## Compatible Support Of Gonacli\n- Linux\n- Mac OS\n- Windows\n\n## Compatible Support Of NodeJS Addon\n- Linux / Mac OS / Windows\n- NodeJS(12.0+)\n- Npm(6.0+)\n- Node-gyp(9.0+)\n- Go(1.14+)\n\n## Use Golang Install\n\u003cp\u003eEnsure that the system is configured with GOPATH environment variables before installation\u003c/p\u003e\n\nLinux or Mac OS\n``` shell\n# .bash_profile\nexport GOPATH=\"/Users/awen/go\"\n# set bin dir\nexport PATH=\"$PATH:$GOPATH:$GOPATH/bin\"\n```\n\nWindow\n``` shell\n# set system path\nGOPATH: C:\\awen\\go\n# set bin dir\nPATH: %GOPATH%\\bin\n``` \n\nInstall\n``` shell\n$ go install github.com/wenlng/gonacli@latest\n\n$ gonacli version\n```\n\u003cbr/\u003e\n\n\n## Compilation Of Windows OS Environment\nIn the Windows OS environment, you need to install the `gcc/g++` compiler support required by Go CGO, download the `MinGW` installation, configure the `PATH` environment variable of `MinGW/bin`, and execute `gcc` normally on the command line.\n\n``` shell\n$ gcc -v\n```\n\nWhen compiling Node Addon in the Windows OS environment, you also need to install the build tool that `node-gyp` and depends on.\n\n``` shell\n$ npm install --global node-gyp\n\n$ npm install --global --production windows-build-tools\n```\n\u003cbr/\u003e\n\n## Gonacli Command\n\n### 1. generate\n\nGenerate bridge code related to NodeJS Addon according to the configuration of goaddon\n\n``` shell\n# By default, it reads the goaddon in the current directory Json configuration file\n$ gonacli generate\n\n# --config: Specify Profile\n$ gonacli generate --config demoaddon.json\n```\n\n### 2. build\n\nSame as the `go build - buildmode=c-archive` command, compile the library\n\n``` shell\n# Compile to generate library\n$ gonacli build\n\n# --args: Specify the args of go build\n# --config: Specify Profile\n$ gonacli build --args '-ldflags \"-s -w\"'\n```\n\n### 3. install\n\nSame as the `npm install`， Install NodeJS dependencies.\n\n``` shell\n# Install dependencies\ngonacli install\n\n# --config: Specify Profile\n$ gonacli install --config demoaddon.json\n```\n\n### 4. msvc\n\nSelect `dlltool.exe` in the Windows OS environment or `lib.exe`\n\n1.`dlltool.exe` of `MinGW`\n\n2.`lib.exe` of `Microsoft Visual c++ Build tools` or `Visual Studio`\n\n``` shell\n$ gonacli msvc\n\n# --vs: use VS \"lib.exe\", default is \"dlltool.exe\" of MinGW\n# --32x: Supports 32-bit OS，default 64.\n# --config: Specify Profile\n$ gonacli msvc --config demoaddon.json\n```\n\n### 5. make\n\nSame as the `node-gyp configure \u0026\u0026 node-gyp build` command，Compile NodeJS Addon\n\n``` text\nPlease ensure that the node gyp compiler has been installed on the system before using the \"make\" command\n\nBefore using the \"--npm-i\" arg, ensure that the system has installed the npm package dependency management tool\n```\n\n``` shell\n# --args: Specify the parameters of node-gyp build，for example \"--debug\"\n$ gonacli make --args '--debug'\n```\n\n\u003cbr/\u003e\n\n## Use Golang to develop an Demo of NodeJS Addon\n\nTip：Ensure that relevant commands can be used normally, This is an demo under Linux/OS environment.\n\n``` shell\n# go\n$ go version\n\n# node\n$ node -v\n\n# npm\n$ npm -v\n\n# node-gyp\n$ node-gyp -v\n```\n\n\n#### 1. Create Goaddon Configure File\n\n`/goaddon.json`\n\n``` json\n{\n  \"name\": \"demoaddon\",\n  \"sources\": [\n    \"demoaddon.go\"\n  ],\n  \"output\": \"./demoaddon/\",\n  \"exports\": [\n    {\n      \"name\": \"Hello\",\n      \"args\": [\n        {\n          \"name\": \"name\",\n          \"type\": \"string\"\n        }\n      ],\n      \"returntype\": \"string\",\n      \"jscallname\": \"hello\",\n      \"jscallmode\": \"sync\"\n    }\n  ]\n}\n```\n\n#### 2. Write Golang Code\n\n`/demoaddon.go`\n\n``` go\npackage main\nimport \"C\"\n\n// notice：//export xxxx is necessary\n\n//export Hello\nfunc Hello(_name *C.char) *C.char {\n\t// args string type，return string type\n\tname := C.GoString(_name)\n\t\n\tres := \"hello\"\n\tif len(name) \u003e 0 {\n\t    res += \",\" + name\n\t}\n\t\n\treturn C.CString(res)\n}\n```\n\n#### 3. Generate Rridging Napi C/C++ Code\n``` shell\n# Save to the \"./demoaddon/\" directory\n$ gonacli generate --config ./goaddon.json\n```\n\n#### 4.Compile Libraries\n``` shell\n# Save to the \"./demoaddon/\" directory\n$ gonacli build\n```\n\n\n#### 5. Install Dependencies\n``` shell\n# Save to the \"./demoaddon/build\" directory\n$ gonacli install\n```\n\n\n#### 6. Compile Nodejs Adddon\n``` shell\n# Save to the \"./demoaddon/build\" directory\n$ gonacli make\n```\n\n#### 7. Create JS Test File\n\n`/demoaddon/test.js`\n\n``` javascript\nconst demoaddon = require('.')\n\nconst name = \"awen\"\nconst res = demoaddon.hello(name)\nconsole.log('\u003e\u003e\u003e ', res)\n\n```\n\n``` shell\n$ node ./test.js\n# \u003e\u003e\u003e hello, awen\n```\n\n\u003cbr/\u003e\n\n## Configure File Description\n``` text\n{\n  \"name\": \"demoaddon\",      // Name of Nodejs Addon\n  \"sources\": [              // File list of go build，Cannot have path\n    \"demoaddon.go\"\n  ],\n  \"output\": \"./demoaddon/\", // Output directory path\n  \"exports\": [              // Exported interface, generating the Napi and C/C++ code of Addon\n    {\n      \"name\": \"Hello\",      // The name of the \"//export Hello\" interface corresponding to Golang must be consistent\n      \"args\": [             // The parameter type of the passed parameter list must be consistent with the type table\n        {                  \n          \"name\": \"name\",\n          \"type\": \"string\"\n        }\n      ],\n      \"returntype\": \"string\",   // The type returned to JavaScript，has no callback type\n      \"jscallname\": \"hello\",    // JavaScript call name\n      \"jscallmode\": \"sync\"      // Sync is synchronous execution, and Async is asynchronous execution\n    },\n    {\n        name: \"xxx\",\n        ....\n    }\n  ]\n}\n```\n\n## Type Table\n\n|    Type     | Golang Args | Golang Return  |   JS / TS   |\n|:-----------:|:-----------:|:--------------:|:-----------:|\n|     int     |    int32    |     C.int      |   number    |\n|    int32    |    int32    |     C.int      |   number    |\n|    int64    |    int64    |   C.longlong   |   number    |\n|   uint32    |   uint32    |     C.uint     |   number    |\n|    float    |   float32   |    C.float     |   number    |\n|   double    |   float64   |    C.double    |   number    |\n|   boolean   |    bool     |      bool      |   boolean   |\n|   string    |   *C.char   |    *C.char     |   string    |\n|    array    |   *C.char   |    *C.char     |    Array    |\n|   object    |   *C.char   |    *C.char     |   Object    |\n|  callback   |   *C.char   |       -        |  Function   |\n\n### The returntype field type of the configuration file\nThe `returntype` field has no callback type\n\n### array type\nWhen there are multiple levels when returning, it is not recommended to use in the `returntype`\n\n\n1. The `array` type received in Golang is a string `*C.Char` type, which needs to be use `[]interface{}` and `json.Unmarshal`\n\n\n2. The `array` type is when Golang returns `*C.Char` type, use `json.Marshal`\n\n\n3. The `array` type is an Array type when JavaScript is passed, but currently only supports one layer when receiving. Please use string method to return multiple layers in Golang, and then use JavaScript's `JSON.parse`\n\n### object type\nWhen there are multiple levels when returning, it is not recommended to use in the `returntype`\n\n1. The `object` type received in Golang is a string type. You need to use `[string]interface{}` and `json.Unmarshal`\n\n\n2. The `object` type is when Golang returns `*C.Char` type, use `json.Marshal`\n\n\n3. The `object` type is an Object type when JavaScript is passed, but currently only supports one layer when receiving. Please use string method to return multiple layers in Golang, and then use JavaScript's `JSON.parse`\n\n\u003cbr/\u003e\n\n## JavaScript Sync Call\n\n`/goaddon.json`\n\n``` json\n{\n  \"name\": \"demoaddon\",\n  \"sources\": [\n    \"demoaddon.go\"\n  ],\n  \"output\": \"./demoaddon/\",\n  \"exports\": [\n    {\n      \"name\": \"Hello\",\n      \"args\": [\n        {\n          \"name\": \"name\",\n          \"type\": \"string\"\n        }\n      ],\n      \"returntype\": \"string\",\n      \"jscallname\": \"hello\",\n      \"jscallmode\": \"sync\"\n    }\n  ]\n}\n```\n\n#### 2. Golang Code\n\n`/demoaddon.go`\n\n``` go\npackage main\nimport \"C\"\n\n//export Hello\nfunc Hello(_name *C.char) *C.char {\n\t// args is string type，return string type\n\tname := C.GoString(_name)\n\t\n\tres := \"hello\"\n\tch := make(chan bool)\n\n\tgo func() {\n\t    // Time consuming task processing\n\t    time.Sleep(time.Duration(2) * time.Second)\n\t\tif len(name) \u003e 0 {\n\t        res += \",\" + name\n\t    }   \n\t\tch \u003c- true\n\t}()\n\n\t\u003c-ch\n\t\n\treturn C.CString(res)\n}\n```\n\n#### 3. Test\n\n`/test.js`\n\n``` javascript\nconst demoaddon = require('./demoaddon')\n\nconst name = \"awen\"\nconst res = demoaddon.hello(name)\nconsole.log('\u003e\u003e\u003e ', res)\n```\n\n\u003cbr/\u003e\n\n## JavaScript Async Call\n\n`/goaddon.json`\n\n``` json\n{\n  \"name\": \"demoaddon\",\n  \"sources\": [\n    \"demoaddon.go\"\n  ],\n  \"output\": \"./demoaddon/\",\n  \"exports\": [\n    {\n      \"name\": \"Hello\",\n      \"args\": [\n        {\n          \"name\": \"name\",\n          \"type\": \"string\"\n        },\n        {\n          \"name\": \"cbs\",\n          \"type\": \"callback\"\n        }\n      ],\n      \"returntype\": \"string\",\n      \"jscallname\": \"hello\",\n      \"jscallmode\": \"async\"\n    }\n  ]\n}\n```\n\n#### 2. Golang Code\n\n`/demoaddon.go`\n\n``` go\npackage main\nimport \"C\"\n\n//export Hello\nfunc Hello(_name *C.char, cbsFnName *C.char) *C.char {\n\t// args is string type，return string type\n\tname := C.GoString(_name)\n\t\n\tres := \"hello\"\n\tch := make(chan bool)\n\n    go func() {\n\t    // Time consuming task processing\n\t\ttime.Sleep(time.Duration(2) * time.Second)\n\t\tif len(name) \u003e 0 {\n\t        res += \",\" + name\n\t    }   \n\t\tch \u003c- true\n\t}()\n\n\t\u003c-ch\n\t\n\treturn C.CString(res)\n}\n```\n\n#### 3. Test\n\n`/test.js`\n\n``` javascript\nconst demoaddon = require('./demoaddon')\n\nconst name = \"awen\"\ndemoaddon.hello(name, funciton(res){\n    console.log('\u003e\u003e\u003e ', res)\n})\n```\n\n\u003cbr/\u003e\n\n## LICENSE\n    MIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwenlng%2Fgonacli","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwenlng%2Fgonacli","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwenlng%2Fgonacli/lists"}