由 Go 托管的 Clojure
Clojure Hosted on Go

原始链接: https://github.com/glojurelang/glojure

Glojure 是一个用 Go 编写的“宿主式” Clojure 解释器,旨在提供两种语言之间的无缝互操作性。与其他实现不同,它允许 Go 值和 Clojure 值相互转换,使开发者能够轻松结合 Go 的性能与 Clojure 的表达性语法。 Glojure 目前处于早期开发阶段,主要支持两种使用场景: * **独立 CLI:** 用于交互式开发、运行脚本或评估单行代码的传统 REPL 环境。它具备多行编辑、Tab 补全和持久化历史记录等高级工具功能。 * **嵌入式脚本:** 开发者可以将 Glojure 直接嵌入到 Go 应用程序中,以提供脚本能力、插件系统或灵活的配置功能。 该项目为许多 Go 标准库包提供了内置的互操作支持,并提供了将自定义 Go 包暴露给 Clojure 运行时的工具。虽然 Glojure 仍在不断发展中,但它已经可以运行 Clojure 核心库的很大一部分,适合个人爱好项目使用。它需要 Go 1.24+ 环境,并通过 `go install` 安装。请注意,作为一个处于早期开发阶段的项目,API 在 v1 版本发布前可能会发生变化。

Hacker News 新内容 | 过往 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 Clojure Hosted on Go (github.com/glojurelang) 由 dnlo 于 1 小时前发布 | 6 分 | 隐藏 | 过往 | 收藏 | 讨论 | 帮助 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

example workflow

Try it in your browser!

Gopher image

Gopher image derived from @egonelbre, licensed under Creative Commons 1.0 Attributions license.

Glojure is an interpreter for Clojure, hosted on Go. Glojure provides easy access to Go libraries, similar to how Clojure provides easy access to Java frameworks.

Glojure is in early development; expect bugs, missing features, and limited performance. Backwards compatibility is not guaranteed until a v1 release. That said, it is used successfully in hobby projects and runs a significant subset of the (transformed) core Clojure library.

Note that unlike most other Go implementations of Clojure, Glojure is a "hosted" language - a term used to describe languages that are implemented in terms of a host language (in this case, Go). This means that all Go values can be used as Glojure values and vice versa.

Before you get started with Glojure, make sure you have installed and have knowledge of Go (version 1.19 or higher).

Glojure is currently available from source for all platforms where Go can run, and it requires at least go 1.24.

Install it with the go install command:

$ go install github.com/glojurelang/glojure/cmd/glj@latest

After installation, you can start the REPL (Read-Eval-Print-Loop) with the glj command:

$ glj
user=> (println "Hello, world!")
Hello, world!
nil
user=>

Glojure can be used in two ways: as a standalone command-line tool (glj) or embedded within Go applications.

The glj command provides a traditional Clojure development experience:

Show the help:

Show the version:

$ glj --version
glojure v0.3.0

Start a REPL (interactive session):

user=> *glojure-version*
{:major 0, :minor 3, :incremental 0, :qualifier nil}
$ glj
user=> (+ 1 2 3)
6
user=> (println "Hello from Glojure!")
Hello from Glojure!
nil

The interactive REPL includes:

  • Vi and emacs editing modes -- vi is the default; configure via ~/.inputrc
  • Multiline editing -- incomplete expressions continue on the next line with auto-indent
  • Tab completion -- symbols, namespaces, and aliases with descriptive labels
  • Smart indentation -- Tab inserts 2 spaces; Backspace removes a full indent level
  • Persistent history -- saved to ~/.glj_history across sessions
  • Bracketed paste -- paste blocks of code instantly
  • Job control -- Ctrl+Z suspends, fg resumes cleanly
  • Interrupt -- Ctrl+C cancels input or interrupts evaluation

Evaluate expressions:

$ glj -e '(println "Hello, World!")'
Hello, World!
$ glj -e '(apply + (range 3 10))'
42
$ glj -e '
(defn factorial [n] (if (<= n 1) 1 (* n (factorial (dec n)))))
(factorial 5)'
120

Run a Clojure script:

;; hello.glj
(println "Hello," (first *command-line-args*))
$ glj hello.glj World
Hello, World

Create executable programs:

;; server.glj
(ns example.server)

(defn echo-handler
  [w r]
  (io.Copy w (.Body r))
  nil)

(net:http.Handle "/" (net:http.HandlerFunc echo-handler))
(println "Server starting on :8080...")
(net:http.ListenAndServe ":8080" nil)
$ glj server.glj
Server starting on :8080...

Embedding Glojure in Go Applications

You can also embed Glojure as a scripting language within your Go applications. This is useful when you want to:

  • Add scriptable configuration to your Go application
  • Allow users to extend your application with Clojure plugins
  • Mix Go's performance with Clojure's expressiveness
  • Control the execution environment (custom I/O, sandboxing)

Basic embedding example:

package main

import (
    "fmt"
    _ "github.com/glojurelang/glojure/pkg/glj"  // Initialize Glojure
    "github.com/glojurelang/glojure/pkg/runtime"
)

func main() {
    // Evaluate Clojure code
    result := runtime.ReadEval(`
        (defn factorial [n]
          (if (<= n 1)
            1
            (* n (factorial (dec n)))))
        (factorial 5)
    `)
    fmt.Printf("5! = %v\n", result) // 5! = 120
}

Calling Go from Clojure and vice versa:

package main

import (
    "fmt"
    "github.com/glojurelang/glojure/pkg/glj"
    "github.com/glojurelang/glojure/pkg/runtime"
)

// Define a Go function
func greet(name string) string {
    return fmt.Sprintf("Hello, %s from Go!", name)
}

func main() {
    // Make the Go function available to Clojure
    runtime.ReadEval(`(def greet-from-go nil)`) // placeholder
    greetVar := glj.Var("user", "greet-from-go")
    greetVar.SetRoot(greet)

    // Use it from Clojure
    result := runtime.ReadEval(`(greet-from-go "Clojure")`)
    fmt.Println(result) // "Hello, Clojure from Go!"

    // Call a Clojure function from Go
    runtime.ReadEval(`(defn add [x y] (+ x y))`)
    addFn := glj.Var("user", "add")
    sum := addFn.Invoke(10, 32)
    fmt.Printf("Sum: %v\n", sum) // Sum: 42
}

Accessing your own Go packages:

When embedding Glojure, you can also expose your own Go packages or additional standard library packages using the package map approach described in the Accessing additional Go packages section below. This allows embedded Clojure code to access any Go packages you choose to expose:

import (
    _ "github.com/glojurelang/glojure/pkg/glj"
    _ "your.app/gljimports" // Your generated package map
)

// Now Clojure code can access your exposed packages
runtime.ReadEval(`
    (your$package.YourFunction "arg")
    (another$package.Method)
`)

When to Use Each Approach

Use glj command for:

  • Writing standalone Clojure programs
  • Interactive development with the REPL
  • Running Clojure scripts
  • Evaluating expressions directly from the command line
  • Learning Clojure with Go interop

Embed Glojure for:

  • Adding scripting to an existing Go application
  • Building a platform that users extend with Clojure
  • Custom control over the Glojure execution environment
  • Mixing Go and Clojure in a single binary

Glojure ships with interop with many standard library packages out-of-the-box. Go package names are munged to avoid ambiguity with the use of / to refer to namespaced symbols; instances of / in package names are replaced with :. Here's a simple example:

user=> (println (fmt.Sprintf "A couple of HTTP methods: %v" [net:http.MethodGet net:http.MethodPost]))
A couple of HTTP methods: ["GET" "POST"]
nil

The following standard library packages are included by default:

  • bytes
  • context
  • errors
  • flag
  • fmt
  • io
  • io/fs
  • io/ioutil
  • math
  • math/big
  • math/rand
  • net/http
  • os
  • os/exec
  • os/signal
  • regexp
  • reflect
  • sort
  • strconv
  • strings
  • sync
  • sync/atomic
  • time
  • unicode

To expose additional packages, you must generate a "package map" and compile your own executable that imports both your package map and the Glojure API. See the section below for more details.

Expect improvements to both the availability of standard library packages and interop workflows.

Accessing additional Go packages

The gen-import-interop can be used to emit the contents of a .go file that will export a function that can be used to add the exports of additional packages to the Glojure package map.

$ go run github.com/glojurelang/glojure/cmd/gen-import-interop \
     -packages=:comma-separated-package-list: \
     > your/package/gljimports/my_package_map.go

Then, in your own program:

package main

import (
	// Add your packages' exports to the pkgmap.
	_ "your.package/gljimports"
)

// ...
Clojure Type Glojure Type Notes
long int64
double float64
float float32
byte byte Note that Go bytes are unsigned, whereas JVM bytes are signed.
short int16
int int Note that JVM ints are 32-bit, whereas Go ints are 32- or 64-bit depending on the platform.
char lang.Char The Glojure type is a tagged rune (type Char rune). JVM chars are 16-bit whereas Go runes are 32-bit.
BigInt *lang.BigInt The Glojure type wraps *big.Int.
BigDecimal *lang.BigDecimal The Glojure type wraps *big.Float.
Ratio *lang.Ratio The Glojure type wraps *big.Rat.
BigInteger *big.Int Native JVM BigInteger corresponds to *big.Int.

Comparisons to other Go ports of Clojure

Aspect Glojure Joker let-go
Hosted1 Yes No No
Extensible Go interop Yes No No
Concurrency Yes Yes (with GIL) Yes
Clojure tooling (e.g. linter) No Yes No
Execution Tree-walk interpreter Tree-walk interpreter Bytecode Interpreter

If you'd like to see another port in this table, or if you believe there is an error in it, please file an issue or open a pull request!

联系我们 contact @ memedata.com