Anaconda developers recently released PyScript, a framework that allows developers to mix Python code into HTML, which some have called “JSP for Python”. In fact, PyScript uses WebAssembly for its underlying layer, as it is built on Pyodide, which consists of a CPython 3.8 interpreter compiled into WebAssembly that allows Python to be run in a web browser.

PyScript

It’s no coincidence that the developers have adopted the same idea to make Go run smoothly on the browser as well. https://goscript.dev The website supports running Go code directly in the browser, a Go playground with Goscript under the hood, implemented via WASM.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// You can edit this code!
// Click here and start typing.
package main

import "fmt"

func fibonacci(c, quit chan int) {
    x, y := 0, 1
    for {
        select {
        case c <- x:
            x, y = y, x+y
        case <-quit:
            fmt.Println("quit!")
            return
        }
    }
}

func main() {
    c := make(chan int)
    quit := make(chan int)
    go func() {
        for i := 0; i < 12; i++ {
            fmt.Println(<-c)
        }
        quit <- 0
    }()

    fibonacci(c, quit)
}

Goscript is an unofficial implementation of the Go language specification for inlining or wrapping Rust projects, providing a simpler way to wrap and call the underlying Rust code. It is like Lua is to Redis/WoW, or Python is to NumPy.

Goscript contains six items.

  • parser turns the source code into an AST, ported from the official Go code.
  • type checker does type pushing and type checking based on AST, also ported from the official Go code.
  • codegen Generate bytecode based on AST and type information.
  • vm Runs bytecode.
  • std Official library, ported from the official Go library.
  • engine Contains the native part of the official library, plus a simple wrapper.

Goscript implements almost all of Go’s features (pre 1.18, so not including generics) and ports some of the official libraries, see https://github.com/oxfeeefeee/goscript/tree/master/engine/tests for passed test cases.

Goscript project code: https://github.com/oxfeeefeee/goscript