Happy happy joy joy

Translate Go into JavaScript with the all-new Joy compiler

Jane Elizabeth
Joy
©  Shutterstock /  Ditty_about_summer

The brand-new Joy compiler brings Go’s classic design to the frontend with JavaScript that works in every browser. Code is written once and read everywhere with this simple Go compiler.

The Joy compiler promises to bring Go’s simplicity to JavaScript with an incredibly simple tool. Now, you can use Go’s stability but still execute in JavaScript, making it easier and simpler to build large web applications with confidence.

Writing and maintaining long-lasting JavaScript is time-consuming, to say the least. Thanks to the big changes in the JavaScript language in the past few years, major breaking changes and cascading problems downstream in in tools and libraries. While JavaScript isn’t going anywhere, it’s become a lot simpler to use with the Joy compiler.

Joy to the world

Based on the theory that typed code is easier to maintain and update and that less syntax is better than prettier syntax, Joy translates Go into JavaScript. So, you can write in the simple Go but still deploy in the incredibly useful JavaScript.

Joy is a fast 1:1 Go to JavaScript compiler, intended to create concise JavaScript from idiomatic Go. It uses Go’s static analysis tools to eliminate dead code and creates JavaScript that works in every browser. It contains a simple macro system for integrating with existing JavaScript. Joy also has first-class support for virtual DOM frameworks like React and Preact.

Joy is open source on GitHub and provides zero-cost typed DOM and virtual DOM packages. It ships a minimal runtime only when it’s needed and avoids unused code as much as possible. Joy embraces the use of standard libraries and tools.

SEE MORE: Java is likely to outlive Go: How can we judge which programming languages will survive?

How it works

The Joy compiler has 5 steps: parse, index, graph, translate and assemble.

  • Parse turns your Go code into an abstract syntax tree (AST). This is primarily done using the x/tools/loader library provided by the Go team.
  • Index processes these Go ASTs allowing us to answer deep questions about our program.
  • Graph uses the index to create a dependency graph. This graph is sorted topologically and pruned, resulting in a list of nodes containing the functions, variables, structs and interfaces that are used in our program.
  • Translate takes the nodes in our graph and the knowledge in our index to translate the Go AST into a JavaScript AST.
  • Assemble takes the resulting JavaScript AST and generates JavaScript code.

Here’s what a simple sleep routing looks like once it’s been through the Joy compiler.

package main

import (
  "time"
)

func main() {
  time.Sleep(1 * time.Second)
}

And now how it looks in JavaScript:

;(function() {
  var pkg = {};
  pkg["58-time-sleep"] = (function() {
    async function main () {
      await new Promise(function(resolve, reject) { setTimeout(resolve, 1 * 1000) })
    };
    return {
      main: main
    };
  })();
  return pkg["58-time-sleep"].main();
})() 

SEE MORE: Write scientific code with Gonum’s numeric libraries for Go

Where can I get it?

Money doesn’t buy happiness, but you can download Joy for free via GitHub or using the following curl command.

curl -sfL https://raw.githubusercontent.com/matthewmueller/joy/master/install.sh | sh

If you have any issues or comments, feel free to share via the Joy slack channel or on twitter.

Author
Jane Elizabeth
Jane Elizabeth is an assistant editor for JAXenter.com

Comments
comments powered by Disqus