8000 GitHub - sunnydanu/go2js: Compile Go to JS in the browser (using Gopherjs)
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

sunnydanu/go2js

 
 

Repository files navigation

Go2JS

Compile Go to JS in the browser (using GopherJS)

Based on GopherJS Playground

The motive for this project was to enable client-side compiling of Golang to JavaScript to run in the browser for LiveCodes.

Usage

UMD:

<!-- <script src="https://cdn.jsdelivr.net/npm/@live-codes/go2js/build/go2js.js"></script> -->
<script src="go2js.js"></script>
<script>
  const code = `
package main
import ("syscall/js"
"fmt")
func main() {fmt.Println("Hello, from Go")
js.Global().Call("alert", "Hello, JavaScript")
println("Hello, JS console")}
`;
  window.go2js
    .format(code)
    .then((formatted) => {
      console.log(formatted);
      // return window.go2js.compile(formatted, 'https://cdn.jsdelivr.net/npm/@live-codes/go2js/build/');
      return window.go2js.compile(formatted);
    })
    .then(eval)
    .catch(console.warn);
</script>

ESM:

import { compile, format } from 'https://cdn.jsdelivr.net/npm/@live-codes/go2js';

const code = `
package main
import ("syscall/js"
"fmt")
func main() {fmt.Println("Hello, from Go")
js.Global().Call("alert", "Hello, JavaScript")
println("Hello, JS console")}
`;

format(code)
  .then((formatted) => {
    console.log(formatted);
    // return compile(formatted, 'https://cdn.jsdelivr.net/npm/@live-codes/go2js/build/');
    return compile(formatted);
  })
  .then(eval)
  .catch(console.warn);

Run npm start to start a server (http://127.0.0.1:8080) and watch for changes.

Contribution

Please do!

Feedback, ideas and pull requests are highly appreciated.

License

BSD 2-Clause "Simplified" License same as GopherJS lisence

Support the author

ko-fi

About

Compile Go to JS in the browser (using Gopherjs)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • CSS 68.8%
  • HTML 19.4%
  • JavaScript 5.4%
  • Go 3.5%
  • TypeScript 1.8%
  • Shell 1.1%
0