explaingit

robertkrimen/otto

8,434GoAudience · developerComplexity · 3/5Setup · moderate

TLDR

A JavaScript interpreter built in Go that lets Go applications run JavaScript code at runtime, making it easy to add user-scriptable logic to any Go program.

Mindmap

mindmap
  root((otto))
    What It Does
      Run JS in Go
      Two-way value bridge
      Scriptable logic
    Features
      Virtual machine API
      CLI tool included
      JS parser package
    Limitations
      ECMAScript 5 only
      No setTimeout
      Regex gaps
    Use Cases
      User customization
      Rules engines
      Sandboxed scripts
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Embed a scripting layer in a Go app so end users can write small JavaScript customizations that run inside the program.

USE CASE 2

Evaluate JavaScript expressions in a Go server to implement a configurable rules engine or formula evaluator.

USE CASE 3

Build a sandboxed script runner with a timeout that safely executes untrusted JavaScript snippets in a Go backend.

USE CASE 4

Parse JavaScript source files into an abstract syntax tree using Otto's parser package for static analysis tooling.

Tech stack

GoJavaScript

Getting it running

Difficulty · moderate Time to first run · 30min

Only supports ECMAScript 5, modern JavaScript features introduced in ES6 and later will not work.

In plain English

Otto is a JavaScript interpreter written in Go, a compiled programming language made by Google. It lets Go programs run JavaScript code directly, without needing a separate JavaScript runtime like Node.js. Developers use it to add scripting capabilities to Go applications, such as letting users write small scripts that the application can evaluate at runtime. The library works by creating a virtual machine object in Go. You pass JavaScript code as a string to that machine, and it runs it. You can also pass values from Go into the JavaScript environment (numbers, strings, functions) and read results back out. This two-way bridge is the main reason people use it: you can write core application logic in Go and expose pieces of it to JavaScript scripts that users or operators might want to customize. There is also a standalone command-line tool included in the repository that lets you run JavaScript files directly from your terminal, without writing any Go code. For developers who want to work with JavaScript's syntax structure rather than executing it, there is a separate parser package that reads JavaScript and produces an abstract syntax tree, which is a data structure that represents the code's structure. Otto targets ECMAScript 5, which is the version of the JavaScript standard from 2009. It does not support most ES6 features introduced in 2015 and later. There are also some known limitations with regular expressions, since Go uses a regex engine that does not support certain patterns that JavaScript code sometimes relies on. Functions like setTimeout and setInterval, which are common in browser JavaScript, are not included because they are not part of the core language standard and require an event loop to work correctly. The README includes a code example for stopping scripts that run too long, which is useful when running untrusted JavaScript. Optional integration with the Underscore.js utility library is also supported.

Copy-paste prompts

Prompt 1
Write a Go program using otto that creates a VM, exposes a custom Go function to JavaScript, runs a user-provided script that calls it, and prints the result.
Prompt 2
How do I use otto to run untrusted JavaScript with a 5-second timeout so a runaway script can't hang my Go server?
Prompt 3
Show me how to pass a Go map into otto's JavaScript environment as an object and read values back from it after the script runs.
Prompt 4
Use otto's parser package to read a JavaScript file and print the names of all functions defined at the top level.
Open on GitHub → Explain another repo

← robertkrimen on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.