Is it possible to embed a language inside Go? I need it to create plugins inside my application.
At the first, I'll explain cgo. Go provides API to export values into C language.
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
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
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 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 goja - ECMAScript 5.1(+) implementation in Go.
At the first, I'll explain cgo. Go provides API to export values into C language.
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
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");
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.