Embed a scripting language inside Go
Asked Answered
K

4

10

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

Kristopher answered 16/12, 2014 at 3:0 Comment(0)
D
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

Dagenham answered 16/12, 2014 at 3:8 Comment(3)
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.Dagenham
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".Dagenham
bits modified explanation. How about this>Dagenham
K
40

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

Update 2:

  • Anko - Golang-like syntax
  • Binder - Seamless lua binding, uses gopher-lua
  • Cel-Go - C-like syntax
  • Ecal - Golang-like syntax
  • Expr - Golang-like syntax
  • Gentee - strong-typed procedural language, used for shell-scripting-like use case
  • Gisp - LISP-like syntax
  • Goja - pure Go, ecmascript 5.1 interpreter
  • gval - math expression evaluator, not a complete language
  • prolog - another prolog interpreter
  • purl - perl interpreter
  • starlark - python-like syntax
Kristopher answered 16/12, 2014 at 3:21 Comment(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?!Lepper
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.Fluvial
M
11

goja - ECMAScript 5.1(+) implementation in Go.

Martelli answered 27/1, 2017 at 10:17 Comment(1)
On average 6-7 times faster than otto. That's substantial.Huckaback
D
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

Dagenham answered 16/12, 2014 at 3:8 Comment(3)
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.Dagenham
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".Dagenham
bits modified explanation. How about this>Dagenham
R
4

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");
Ribbing answered 17/7, 2021 at 0:23 Comment(1)
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: 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.Fluvial

© 2022 - 2024 — McMap. All rights reserved.