9

Is it possible to embed a language inside Go? I need it to create plugins inside my application.

apaderno
  • 28,547
  • 16
  • 75
  • 90
Kokizzu
  • 24,974
  • 37
  • 137
  • 233

4 Answers4

37

I found the list on Virtual Machines and Languages.

  • Gelo - Extensible, embeddable interpreter
  • GoForth - A simple Forth parser
  • GoLightly - A flexible and lightweight virtual machine with runtime-configurable instruction set
  • Golog - Prolog interpreter in Go
  • Minima - A language implemented in Go.
  • RubyGoLightly - An experimental port of TinyRb to Go
  • forego - Forth virtual machine
  • go-python - go bindings for CPython C-API
  • GoEmPHP - This package is built for Embedding PHP into Go.
  • goenv - Create an isolated environment where you install Go packages, binaries, or even C libraries. Very similar to virtualenv for Python.
  • golemon - A port of the Lemon parser-generator
  • goll1e - An LL(1) parser generator for the Go programming language.
  • golua - Go wrapper for LUA's C API
  • golua-fork - A fork of GoLua that works on current releases of Go
  • gotcl - Tcl interpreter in Go
  • ngaro - An ngaro virtual machine to run retroForth images
  • otto - A JavaScript parser and interpreter written natively in Go
  • monkey - Embed SpiderMonkey, the Mozilla JavaScript engine, in your Go program.
  • go-v8 - V8 JavaScript engine bindings for Go
  • gomruby - mruby (mini Ruby) bindings for Go
  • LispEx - A dialect of Lisp extended to support for concurrent programming, written in Go.

Update:

  • Tengo - a small, dynamic, fast, secure script language for Go. (similar syntax with Go)
  • glua, GoLuaJit, gijit, and others - LuaJIT, one of fastest JIT implementation
  • Elsa - Typescript and Javascript, based on QuickJS, same person who creates qemu, ffmpeg, tcc
Kokizzu
  • 24,974
  • 37
  • 137
  • 233
  • 2
    From your link, `meme` is written in C. **EDIT**: nevermind, I see now the `go` and `go1` branches - the author seems to be writing it in both languages?! – Renato Mar 13 '19 at 17:36
  • 1
    At the time of writing this comment, `Golog` unfortunately was archived by the maintainers. Also, it would be great to list which are Pure Go implementations, and which require CGO to launch external C libraries. – Gwyneth Llewelyn Nov 16 '21 at 15:52
11

goja - ECMAScript 5.1(+) implementation in Go.

funny_falcon
  • 427
  • 3
  • 11
6

At the first, I'll explain cgo. Go provides API to export values into C language.

http://golang.org/cmd/cgo/

For example, you can export string as char* like below.

package main

/*
#include <stdio.h>
static void myputs(char* s) {
    puts(s);
}
*/
import "C"

func main() {
    s := "hello world"
    C.myputs(C.CString(s))
}

So you need to write functions to access C library. But there are some packages to use script languages. See:

https://github.com/mattn/go-mruby

https://github.com/mattn/go-v8

Or if you don't want to use C language. You can use native go language like otto

https://github.com/robertkrimen/otto

https://github.com/mattn/anko

mattn
  • 7,571
  • 30
  • 54
  • 1
    No one says whether go is scripting language or not. Question is how to embed scripting languages in go. For example, the code above read a file and execute, it will be runtime of scripting language. You can see go-mruby, go-v8, otto, anko above. – mattn Jan 18 '17 at 06:11
  • This is not explaining about go-mruby. Just explaining what cgo is. See this entry "Or if you don't want to use C language. You can use native go language like otto". – mattn Jan 19 '17 at 03:35
  • 1
    bits modified explanation. How about this> – mattn Jan 19 '17 at 10:46
3

One that is very nice to use, but wasn't mention above: gopher-lua, a Lua 5.1 VM:

L := lua.NewState()
defer L.Close()
_ = L.DoString(`print("hello")`);
_ = L.DoFile("hello.lua");
Ibraheem Ahmed
  • 11,652
  • 2
  • 48
  • 54
  • `gopher-lua` is indeed very nice to use, especially taking into account that is a Pure Go implementation (no CGO, no external C libraries to link) which should run anywhere where Go is supported. Additionally, you can add a layer to simplify most calls to and from GopherLua: https://layeh.com/gopher-luar; this allows using Go channels in Lua, for example. Both packages get reasonably regular updates. However, `gopher-lua` is currently restricted to Lua 5.1. If you absolutely need another version of Lua, you'll have to stick to one of the packages based on popular C libraries. – Gwyneth Llewelyn Nov 16 '21 at 15:50