{"id":20459734,"url":"https://github.com/rosbit/go-wasm","last_synced_at":"2025-09-24T20:31:34.782Z","repository":{"id":57706823,"uuid":"500670081","full_name":"rosbit/go-wasm","owner":"rosbit","description":"makes creating WebAssembly with Golang easily","archived":false,"fork":false,"pushed_at":"2023-07-03T10:21:40.000Z","size":15,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-11-15T12:17:04.276Z","etag":null,"topics":["go-wasm","golang","wasm","webassembly","wrapper"],"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/rosbit.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}},"created_at":"2022-06-07T03:02:52.000Z","updated_at":"2024-11-10T21:41:43.000Z","dependencies_parsed_at":"2024-06-21T01:39:18.257Z","dependency_job_id":null,"html_url":"https://github.com/rosbit/go-wasm","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rosbit%2Fgo-wasm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rosbit%2Fgo-wasm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rosbit%2Fgo-wasm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rosbit%2Fgo-wasm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rosbit","download_url":"https://codeload.github.com/rosbit/go-wasm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234123753,"owners_count":18783475,"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":["go-wasm","golang","wasm","webassembly","wrapper"],"created_at":"2024-11-15T12:17:07.736Z","updated_at":"2025-09-24T20:31:34.421Z","avatar_url":"https://github.com/rosbit.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-wasm, makes creating WebAssembly with golang easily\n\n`go-wasm` is a package extending the golang package `syscall/js` and making creating WebAssembly with golang easily.\nIf you want to learn how to create WebAssembly app with Golang, read [this tutorial](https://github.com/golang/go/wiki/WebAssembly).\n\n### Usage\n\nThe package is fully go-getable, so, just type\n\n  `go get github.com/rosbit/go-wasm`\n\nto install.\n\nAll the following Wasm result should be built with\n\n  **GOOS=js GOARCH=wasm go build**\n\n#### 1. Evaluate expressions\n\n```go\npackage main\n\nimport (\n  \"github.com/rosbit/go-wasm\"\n  \"fmt\"\n)\n\nfunc main() {\n  res, _ := wasm.Eval(\"1 + 2\", nil)\n  fmt.Println(\"result is:\", res)\n}\n```\n\n#### 2. Go calls JS function\n\nSuppose there's a JS file named `a.js` like this:\n\n```js\nfunction add(a, b) {\n    return a+b\n}\n```\n\none can call the JS function `add()` in Go code like the following:\n\n```go\npackage main\n\nimport (\n  \"github.com/rosbit/go-wasm\"\n  \"fmt\"\n)\n\nvar add func(int, int)int\n\nfunc main() {\n  if err := wasm.BindFunc(\"add\", \u0026add); err != nil {\n     fmt.Printf(\"%v\\n\", err)\n     return\n  }\n\n  res := add(1, 2)\n  fmt.Println(\"result is:\", res)\n}\n```\n\n#### 3. JS calls Go function\n\nJS calling Go function is also easy. In the Go code, make a Golang function\nas JS built-in function by calling `MakeJsFunc(\"funcname\", function)`. There's the example:\n\n```go\npackage main\n\nimport \"github.com/rosbit/go-wasm\"\n\n// function to be called by JS\nfunc adder(a1 float64, a2 float64) float64 {\n    return a1 + a2\n}\n\nfunc main() {\n  wasm.MakeJsFunc(\"adder\", adder) // now \"adder\" is a global JS function\n  done := make(chan struct{})\n  \u003c-done\n}\n```\n\nIn JS code, one can call the registered function directly. There's the example `b.js`.\n\n```js\nr = adder(1, 100)   // the function \"adder\" is implemented in Go\nconsole.log(r)\n```\n\n#### 4. Make Go struct instance as a JS object\n\nThis package provides a function `SetModule` which will convert a Go struct instance into\na JS object. There's the example `c.js`, `m` is the object var provided by Go code:\n\n```js\nm.incAge(10)\nconsole.log(m)\n\nconsole.log('m.name', m.name)\nconsole.log('m.age', m.age)\n```\n\nThe Go code is like this:\n\n```go\npackage main\n\nimport \"github.com/rosbit/go-wasm\"\n\ntype M struct {\n   Name string\n   Age int\n}\nfunc (m *M) IncAge(a int) {\n   m.Age += a\n}\n\nfunc main() {\n  wasm.BindObject(\"m\", \u0026M{Name:\"rosbit\", Age: 1}) // \"m\" is the object var name\n\n  done := make(chan struct{})\n  \u003c-done\n}\n```\n\n#### 5. Set many built-in functions and objects at one time\n\nIf there're a lot of functions and objects to be registered, a map could be constructed with function `SetGlobals` or put as an\nargument for function `Eval`.\n\n```go\npackage main\n\nimport \"github.com/rosbit/go-wasm\"\nimport \"fmt\"\n\ntype M struct {\n   Name string\n   Age int\n}\nfunc (m *M) IncAge(a int) {\n   m.Age += a\n}\n\nfunc adder(a1 float64, a2 float64) float64 {\n    return a1 + a2\n}\n\nfunc main() {\n  vars := map[string]interface{}{\n     \"m\": \u0026M{Name:\"rosbit\", Age:1}, // to JS object\n     \"adder\": adder,                // to JS built-in function\n     \"a\": []int{1,2,3},             // to JS array\n  }\n\n  wasm.SetGlobals(vars)\n  res := wasm.GetGlobal(\"a\") // get the value of var named \"a\". Any variables in script could be get by GetGlobal\n  fmt.Printf(\"res:\", res)\n}\n```\n\n#### 6. Wrap anything as JS global object\n\nThis package also provides a function `SetGlobalObject` which will create a JS variable integrating any\nGo values/functions as an object. There's the example `d.js` which will use object `tm` provided by Go code:\n\n```js\na = tm.newA(\"rosbit\", 10)\na.incAge(10)\nconsole.log(a)\n\ntm.printf('a.name: %s\\n', a.name)\ntm.printf('a.age: %d\\n', a.age)\n```\n\nThe Go code is like this:\n\n```go\npackage main\n\nimport (\n  \"github.com/rosbit/go-wasm\"\n  \"fmt\"\n)\n\ntype A struct {\n   Name string\n   Age int\n}\nfunc (m *A) IncAge(a int) {\n   m.Age += a\n}\nfunc newA(name string, age int) *A {\n   return \u0026A{Name: name, Age: age}\n}\n\nfunc main() {\n  wasm.SetGlobalObject(\"tm\", map[string]interface{}{ // object name is \"tm\"\n     \"newA\": newA,            // make user defined function as object method named \"tm.newA\"\n     \"printf\": fmt.Printf,    // make function in a standard package named \"tm.printf\"\n  })\n\n  done := make(chan struct{})\n  \u003c-done\n}\n```\n\n### Other helper functions\n\n```go\nfunc JSONStringify(v interface{}) string    // wasm.JSONStringify([]int{1, 3})\nfunc JSONParse(val string) interface{}      // wasm.JSONParse(`{\"a\":\"b\",\"c\":1}`)\nfunc CallObjectMethod(objName, objMethod string, args ...interface{}) js.Value\n // jsObj := wasm.CallObjectMethod(\"document\", \"getElementById\", \"id\")\nfunc CallFunc(funcName string, args ...interface{}) js.Value // wasm.CallFunc(\"add\", 1, 1)\n```\n\n### Status\n\nThe package is not fully tested, so be careful.\n\n### Contribution\n\nPull requests are welcome! Also, if you want to discuss something send a pull request with proposal and changes.\n\n__Convention:__ fork the repository and make changes on your fork in a feature branch.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frosbit%2Fgo-wasm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frosbit%2Fgo-wasm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frosbit%2Fgo-wasm/lists"}