{"id":13748395,"url":"https://github.com/mcuadros/go-candyjs","last_synced_at":"2025-05-09T10:32:39.669Z","repository":{"id":33175448,"uuid":"36816604","full_name":"mcuadros/go-candyjs","owner":"mcuadros","description":"fully transparent bridge between Go and the JavaScript","archived":true,"fork":false,"pushed_at":"2017-10-05T05:18:55.000Z","size":1097,"stargazers_count":452,"open_issues_count":2,"forks_count":18,"subscribers_count":20,"default_branch":"master","last_synced_at":"2025-05-05T06:07:59.362Z","etag":null,"topics":[],"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/mcuadros.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}},"created_at":"2015-06-03T16:36:48.000Z","updated_at":"2025-02-19T17:33:31.000Z","dependencies_parsed_at":"2022-07-31T10:48:01.785Z","dependency_job_id":null,"html_url":"https://github.com/mcuadros/go-candyjs","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcuadros%2Fgo-candyjs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcuadros%2Fgo-candyjs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcuadros%2Fgo-candyjs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcuadros%2Fgo-candyjs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mcuadros","download_url":"https://codeload.github.com/mcuadros/go-candyjs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253234179,"owners_count":21875562,"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":[],"created_at":"2024-08-03T07:00:40.839Z","updated_at":"2025-05-09T10:32:34.652Z","avatar_url":"https://github.com/mcuadros.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"go-candyjs [![Build Status](https://travis-ci.org/mcuadros/go-candyjs.png?branch=master)](https://travis-ci.org/mcuadros/go-candyjs) [![Coverage Status](https://coveralls.io/repos/mcuadros/go-candyjs/badge.svg?branch=master)](https://coveralls.io/r/mcuadros/go-candyjs?branch=master) [![GoDoc](http://godoc.org/github.com/mcuadros/go-candyjs?status.png)](http://godoc.org/github.com/mcuadros/go-candyjs) [![GitHub release](https://img.shields.io/github/release/mcuadros/go-candyjs.svg)](https://github.com/mcuadros/go-candyjs/releases)\n==========\n\n*CandyJS* is an intent of create a fully **transparent bridge between Go and the\nJavaScript** engine [duktape](http://duktape.org/). Basicly is a syntax-sugar\nlibrary built it on top of [go-duktape](https://github.com/olebedev/go-duktape)\nusing reflection techniques.\n\n#### ok but what for ...\n\nbuild extensible applications that allow to the user **execute** arbitrary\n**code** (let's say plugins) **without** the requirement of **compile** it. \n\nDemo\n----\n\n[![asciicast](https://raw.githubusercontent.com/mcuadros/go-candyjs/master/examples/demo/cast.gif)](https://asciinema.org/a/21430)\n\nFeatures\n--------\nEmbeddable **Ecmascript E5/E5.1 compliant** engine ([duktape](http://duktape.org/)).\n```go\nctx := candyjs.NewContext()\nctx.EvalString(`\n  function factorial(n) {\n    if (n === 0) return 1;    \n    return n * factorial(n - 1);\n  }\n\n  print(factorial(10));\n`)  //3628800\n```\n\nCall **Go functions** from JavaScript and vice versa.\n```go\nctx := candyjs.NewContext()\nctx.PushGlobalGoFunction(\"golangMultiply\", func(a, b int) int {\n    return a * b\n})\n\nctx.EvalString(`print(golangMultiply(5, 10));`) //50\n```\n\nTransparent interface between **Go structs** and JavaScript.\n```go\ntype MyStruct struct {\n    Number int\n}\n\nfunc (m *MyStruct) Multiply(x int) int {\n    return m.Number * x\n}\n...\nctx := candyjs.NewContext()\nctx.PushGlobalStruct(\"golangStruct\", \u0026MyStruct{10})\n\nctx.EvalString(`print(golangStruct.number);`) //10\nctx.EvalString(`print(golangStruct.multiply(5));`) //50\n```\n\nImport of **Go packages** into the JavaScript context.\n```go\n//go:generate candyjs import fmt\n...\nctx := candyjs.NewContext()\nctx.EvalString(`\n    var fmt = CandyJS.require('fmt');\n    fmt.printf('candyjs is %s', 'awesome')\n`) // 'candyjs is awesome'\n```\n\n\nInstallation\n------------\n\nThe recommended way to install go-candyjs is:\n\n```\ngo get -u github.com/mcuadros/go-candyjs/...\n```\n\n\u003e *CandyJS* includes a binary tool used by [go generate](http://blog.golang.org/generate),\nplease be sure that `$GOPATH/bin` is on your `$PATH`\n\n\nExamples\n--------\n\n### JavaScript running a HTTP server \n\nIn this example a [`gin`](https://github.com/gin-gonic/gin) server is executed\nand a small JSON is server. In CandyJS you can import Go packages directly if\nthey are [defined](https://github.com/mcuadros/go-candyjs/blob/master/examples/complex/main.go#L10:L13)\npreviously on the Go code. \n\n**Interpreter code** (`main.go`)\n\n```go\n...\n//go:generate candyjs import time\n//go:generate candyjs import github.com/gin-gonic/gin\nfunc main() {\n    ctx := candyjs.NewContext()\n    ctx.PevalFile(\"example.js\")\n}\n\n```\n\n**Program code** (`example.js`)\n\n```js\nvar time = CandyJS.require('time');\nvar gin = CandyJS.require('github.com/gin-gonic/gin');\n\nvar engine = gin.default();\nengine.get(\"/back\", CandyJS.proxy(function(ctx) {\n  var future = time.date(2015, 10, 21, 4, 29 ,0, 0, time.UTC);\n  var now = time.now();\n\n  ctx.json(200, {\n    future: future.string(),\n    now: now.string(),\n    nsecs: future.sub(now)\n  });\n}));\n\nengine.run(':8080');\n```\n\nCaveats\n-------\nDue to an [incompatibility](https://github.com/svaarala/duktape/issues/154#issuecomment-87077208) with Duktape's error handling system and Go, you can't throw errors from Go. All errors generated from Go functions are generic ones `error error (rc -100)`\n\nLicense\n-------\n\nMIT, see [LICENSE](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmcuadros%2Fgo-candyjs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmcuadros%2Fgo-candyjs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmcuadros%2Fgo-candyjs/lists"}