Goja: A Golang JavaScript Runtime
Goja is a JavaScript runtime library for Go, enabling easy integration and manipulation of data, supporting exception handling, sandboxing, and optimizing performance through sync.Pool for VM initialization.
Read original articleGoja is a JavaScript runtime library designed for the Go programming language, enabling seamless integration of JavaScript within Go applications. It addresses challenges faced when manipulating large datasets, particularly with complex JSON responses. The author initially considered other scripting languages like Lua, Expr, V8, and Starlark but found Goja to be the most effective due to its ability to automatically infer Go struct fields and methods, allowing for straightforward interaction between Go and JavaScript. The article provides examples demonstrating how to pass data between Go and JavaScript, handle exceptions, and manage Go structs within the JavaScript environment. It also discusses the use of a sync.Pool to optimize the initialization of Goja VMs, which can be resource-intensive. The author emphasizes the importance of sandboxing user-provided code to avoid variable declaration conflicts, suggesting the use of anonymous functions to encapsulate user scripts. Overall, Goja offers a flexible and efficient solution for embedding JavaScript in Go applications, enhancing performance and user experience.
- Goja allows seamless integration of JavaScript in Go applications.
- It simplifies data manipulation and interaction between Go and JavaScript.
- The library automatically infers Go struct fields and methods for easy access.
- Goja supports exception handling and sandboxing of user code.
- Using sync.Pool can optimize the performance of Goja VM initialization.
Related
Show HN: Gosax – A high-performance SAX XML parser for Go
The `gosax` Go library enables efficient XML SAX parsing with read-only features, high performance, SWAR optimizations, and `encoding/xml` compatibility. Installation via `go get` and contributions on GitHub are encouraged.
Rustgo: Calling Rust from Go with near-zero overhead
An experiment explores calling Rust code from Go to enhance performance in critical applications. It proposes linking Rust directly into Go, avoiding cgo overhead, and details calling conventions for efficient integration.
Go is my hammer, and everything is a nail
Markus, a solo developer, exclusively uses Go for software development, valuing its simplicity and versatility. He believes focusing on one language enhances productivity and deepens expertise without limiting career options.
An unordered list of things I miss in Go
The blog post highlights features missing in Go, such as ordered maps, default arguments, and improved nullability, suggesting these enhancements could benefit the language despite requiring significant revisions.
Golang Interpreter Written in PHP
Go-PHP is a Golang interpreter for PHP, allowing Go code execution within PHP. It is a work in progress, not for production, with sequential execution and examples available in the repository.
So, Sobek seems to be the way forward...
[1] https://github.com/grafana/sobek/
If you're interested in some production code with Goja, this is our code for calling RoughJS ([1]) from Go in order to produce the hand-drawn diagram look: [2]
[0] https://github.com/terrastruct/d2
[2] https://github.com/terrastruct/d2/blob/master/d2renderers/d2...
Pocketbase is the framework that makes me want to switch to golang. It just makes a lot of sense.
One thing that concerned me, though. How do I debug goja? It doesn't seem like I can set breakpoints as usual.
Or is the point of it to be able to use existing JS scripts within a Go application?
I guess it's the Lisp Curse or something, I dunno.
The article makes no comparisons seemingly? I don't think there's even a never. I find that pretty odd.
I'm curious if there'd be any reason to switch.
Related
Show HN: Gosax – A high-performance SAX XML parser for Go
The `gosax` Go library enables efficient XML SAX parsing with read-only features, high performance, SWAR optimizations, and `encoding/xml` compatibility. Installation via `go get` and contributions on GitHub are encouraged.
Rustgo: Calling Rust from Go with near-zero overhead
An experiment explores calling Rust code from Go to enhance performance in critical applications. It proposes linking Rust directly into Go, avoiding cgo overhead, and details calling conventions for efficient integration.
Go is my hammer, and everything is a nail
Markus, a solo developer, exclusively uses Go for software development, valuing its simplicity and versatility. He believes focusing on one language enhances productivity and deepens expertise without limiting career options.
An unordered list of things I miss in Go
The blog post highlights features missing in Go, such as ordered maps, default arguments, and improved nullability, suggesting these enhancements could benefit the language despite requiring significant revisions.
Golang Interpreter Written in PHP
Go-PHP is a Golang interpreter for PHP, allowing Go code execution within PHP. It is a work in progress, not for production, with sequential execution and examples available in the repository.