goalchemy

package module
v0.7.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

README

Goalchemy

Goalchemy is a transpiler for a restricted, Go-compatible language. You write a program once as ordinary .go files, and Goalchemy lowers it to Go, TypeScript, Python, Java, C#, Rust, C, and Swift with the same observable behavior on every target.

The source is plain Go: it builds, runs, and tests with the standard Go toolchain, and needs no new syntax or custom parser. Goalchemy accepts the subset of Go described in the language specification and rejects everything else with a clear diagnostic.

Why

Shipping the same logic as an SDK in several languages usually means maintaining several hand-written ports that drift apart. Goalchemy keeps one typed source of truth and generates each port from it. The motivating workload is the OpenTDF SDK, whose encryption and decryption logic needs to behave identically across languages.

Scope

Goalchemy is a framework for building one SDK that ships in several languages. You write the SDK's logic once, in a restricted subset of Go, and Goalchemy generates the Go, TypeScript, Python, Java, C#, Rust, C, and Swift packages from it with the same behavior.

It is not a general-purpose transpiler. It won't convert arbitrary Go programs, and it doesn't try to reproduce the whole Go standard library on every target.

The runtime library is deliberately small:

  • Goalchemy provides the language runtime, a set of pure-logic std/ packages (strings, conversions, sorting, encodings), and a few host capabilities that most SDKs need (crypto, HTTP, clocks, logging).
  • Each SDK is designed to provide everything specific to its domain, including its own capabilities: declared with Go signatures and contracts, implemented natively per target, and checked against the same contracts and tests as Goalchemy's own.

If a feature is useful to only one SDK, it belongs in that SDK, not in Goalchemy.

How it works

  1. Load and validate. Packages are loaded and type-checked with go/packages and go/types, then checked against the language gate. Unsupported constructs fail closed with a stable code, source span, and one-line remedy.
  2. Lower to a typed IR. A project-owned intermediate representation makes evaluation order, value copies, aliasing, and runtime operations explicit, so emitters never decide semantics.
  3. Emit and link. Each target emitter writes source code plus only the runtime files the program needs. Every runtime function has one implementation file per target and a canonical YAML contract that defines its behavior.
  4. Verify. Differential tests compare native Go, lowered Go, and every other target against the same fixtures and contract cases.

Notable semantics that hold on every target:

  • int and uint are always 64-bit, with Go's wrapping and conversion rules.
  • float32 and float64 retain their IEEE precision, including signed zero, NaN, infinity, numeric conversions and float values in collections and exported APIs. See float semantics and verification.
  • Strings are immutable byte sequences; slices and maps keep Go's sharing and aliasing.
  • Structs and arrays are copied by value, even on hosts that share objects by default.
  • Errors are values; panics, defer, and recover follow Go's rules.
  • Concurrency (go, channels, select, sync, context) runs on a deterministic cooperative scheduler under the cooperative gate.

Install

Goalchemy is built with the Go 1.27.1 reference toolchain:

go install github.com/eugenioenko/goalchemy/cmd/goalchemy@latest

Or from a checkout:

go build -o bin/goalchemy ./cmd/goalchemy

Quick start

mkdir hello && cd hello
go mod init example.com/hello
go get github.com/eugenioenko/goalchemy@latest
cat > main.go <<'GO'
package main

func main() {
    println("Hello from Goalchemy")
}
GO

goalchemy check .
goalchemy run -target python .
goalchemy compile -target rust -out out/rust .

The go get makes Goalchemy's packages resolvable from your module. Source modules must declare go 1.25 or earlier. Programs print with Go's print and println builtins.

Runtime library

The Go standard library is replaced by packages that keep the standard names, so code still reads and runs as ordinary Go:

  • std/: pure-logic packages (strings, strconv, bytes, io, sort, unicode, unicode/utf8, encoding/hex, encoding/binary, encoding/json (Marshal, Unmarshal, Encoder, Decoder), encoding/jsonvalue, encoding/json/jcs (RFC 8785 canonical JSON), encoding/protojson, connect, time, errors, fmt, log/slog, os, net/url, and net for JoinHostPort and SplitHostPort) written once in the Goalchemy subset and compiled with your program. They behave identically on every target with no native dependencies.
  • lib/: capability packages that reach the host (crypto, http, encoding, checksum, clock, log, os, callback, sync, context, time, errors). Each has a native implementation per target, checked against a contract.

The runtime library reference explains the split and documents every function. protoc-gen-goalchemy generates subset-Go messages with proto3 JSON encoding and Connect service clients from .proto files. The IEEE CRC32 host mappings distinguish standard APIs, the official Microsoft package, and portable fallbacks.

Planned runtime additions

These are the gaps most SDKs and programs hit today, in priority order. Each lands in std/ when it is pure logic and in lib/ when it needs the host.

Addition Root Notes
fmt: Print, Println lib/ Writes to stdout through a small host capability.
os.Getenv lib/ Reports "not set" where the host has no environment, such as browsers.
os.Args, os.Exit, stdin lib/ For programs and examples; SDKs rarely need them.

Domain formats such as ZIP archives for OpenTDF are candidates for the SDK that needs them rather than for Goalchemy, per the scope above.

CLI

Command Purpose
goalchemy check [packages] Load, type-check, and validate against the language gate.
goalchemy compile -target <name> -out <dir> [packages] Write a complete, runnable target directory. -target ir dumps the IR.
goalchemy run -target <name> [-keep] [packages] Compile to a temporary directory and run the program.
goalchemy build [-config goalchemy.yaml] Compile every target listed in a project configuration.
goalchemy docs -format json|markdown|site -out <dir> [packages] Generate consumer API references and an optional Starlight site.
goalchemy features Print supported language features per gate and target.
goalchemy spec validate / spec generate [-check] Validate the contract catalog and regenerate target specs.
goalchemy test Run runtime contract cases through each target's harness.
goalchemy version Print the compiler version.

check, compile, and run share these flags:

Flag Meaning
-gate sequential|cooperative Language gate; cooperative enables tasks, channels, and select.
-tags a,b Build tags used when loading packages.
-C <dir> Directory to load packages from.
-json Write diagnostics as JSON.

Generated source keeps Go identifiers by default on all targets: types, functions, globals and locals retain their source names, and a name is renamed only on a collision (a reserved word, a shadowed local, or the same name in two packages). Compiler-created names start with _, such as the task parameter _t and temporaries like _4.

compile, run, and build accept --names=source|readable|compact (and names: in goalchemy.yaml). Source names, the default, keep Go identifiers and rename only on a collision; --name-collisions=fail reports collisions instead. --compact-names remains a shorthand for --names=compact. Public library names and runtime behavior stay the same in every mode. See generated naming conventions for the limits of these names.

A project can list its targets in goalchemy.yaml and build them all at once:

schema_version: 1
packages: [.]
gate: sequential
targets:
  go: {out: out/go}
  typescript: {out: out/ts}
  python: {out: out/py}
  rust: {out: out/rs}

Non-main packages compile to value libraries for every target. See the usage guide for library boundaries and output layout.

Targets

Target Output To run generated programs
Go main.go, go.mod Go 1.25 or later
TypeScript main.ts (ES2022) Node.js 22.6 or later
Python main.py Python 3.10 or later
Java Main.java, run.sh JDK 21 or later
C# Main.cs, main.csproj .NET SDK 8
Rust src/main.rs, Cargo.toml Stable Rust, edition 2021
C main.c, run.sh C17 compiler and bdwgc 8.x with threads
Swift (experimental) main.swift, run.sh Swift 6.4, C compiler, pkg-config, OpenSSL 3, zlib, libcurl; Linux x86_64

Each output directory also contains its runtime files, a README.md, a line map back to the Go source, and a goalchemy.manifest.json recording the compiler version, contracts, and files. Output is deterministic for the same inputs.

Swift libraries emit a SwiftPM product named GoalchemyGenerated with typed public values and synchronous or asynchronous operations. All Swift outputs currently include native dependency prerequisites. See Swift setup and library use; Apple SDK integration has not yet been verified.

Examples

The examples/ directory has complete programs with a goalchemy.yaml for all targets:

  • bank: typed errors, interfaces, and deferred audit logging
  • calc: an integer expression parser and evaluator
  • fetch: downloads a CSV dataset over HTTPS and summarizes it
  • jwt: signs and verifies HS256 JSON Web Tokens
  • life: Conway's Game of Life on a toroidal board
  • pipeline: workers, channels, select, and per-job deadlines on the cooperative scheduler
  • wordfreq: word counting and ranking
goalchemy run -target python ./examples/calc
cd examples/bank && goalchemy build

Documentation

The public documentation is at goalchemy.dev. The Astro Starlight site lives in docs-web/; see its maintenance guide for local development and GitHub Pages deployment.

Topic Reference
Commands, configuration, output, libraries Usage guide
Consumer API catalog, Markdown, and Starlight template API documentation
Runtime library: std/ and lib/ packages Library reference
Protobuf messages, proto3 JSON and Connect clients Protobuf
Accepted language and semantics Language specification
Diagnostic codes Diagnostics
Supported feature matrix specs/features.yaml
Runtime and type contracts specs/runtime, specs/types
Host lifecycles per target Host operations
Implementing a new target language Adding a language
Swift setup, representation, libraries and verification Swift target
Testing, toolchains, reports Hardening
Known limits and future work Follow-ups
Design and roadmap Implementation plan

Per-target details live in docs/: library boundaries, byte storage, and host operations for TypeScript, Python, Java, C#, Rust, C, and Swift.

License

Goalchemy is licensed under Apache-2.0.

Documentation

Overview

Package goalchemy embeds the contract catalog and target runtimes so the compiler binary is self-contained.

Index

Constants

This section is empty.

Variables

View Source
var Assets embed.FS

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis
cmd
goalchemy command
Command goalchemy validates and transpiles Goalchemy programs.
Command goalchemy validates and transpiles Goalchemy programs.
hardening command
Command hardening runs bounded differential campaigns, performance baselines, and versioned upgrade checks for all released targets.
Command hardening runs bounded differential campaigns, performance baselines, and versioned upgrade checks for all released targets.
protoc-gen-goalchemy command
Command protoc-gen-goalchemy is a protoc and buf plugin that generates Goalchemy-subset Go messages with proto3 JSON encoding and Connect clients for unary methods.
Command protoc-gen-goalchemy is a protoc and buf plugin that generates Goalchemy-subset Go messages with proto3 JSON encoding and Connect clients for unary methods.
examples
bank command
Command bank models accounts with typed errors, interfaces, and deferred audit logging.
Command bank models accounts with typed errors, interfaces, and deferred audit logging.
calc command
Command calc parses and evaluates integer arithmetic expressions.
Command calc parses and evaluates integer arithmetic expressions.
fetch command
Command fetch downloads the iris dataset over HTTPS and summarizes it.
Command fetch downloads the iris dataset over HTTPS and summarizes it.
jwt command
Command jwt signs and verifies HS256 JSON Web Tokens.
Command jwt signs and verifies HS256 JSON Web Tokens.
life command
Command life runs Conway's Game of Life on a fixed toroidal board.
Command life runs Conway's Game of Life on a fixed toroidal board.
pipeline command
Command pipeline fans jobs out to workers under per-job deadlines.
Command pipeline fans jobs out to workers under per-job deadlines.
wordfreq command
Command wordfreq counts words in a text and prints the most frequent ones.
Command wordfreq counts words in a text and prints the most frequent ones.
internal
apidoc
Package apidoc extracts source documentation and native API declarations.
Package apidoc extracts source documentation and native API declarations.
catalog
Package catalog locates the contract catalog and exposes the external capability registry derived from it.
Package catalog locates the contract catalog and exposes the external capability registry derived from it.
conformance
Package conformance runs canonical contract cases against a target's runtime through its JSON Lines harness process.
Package conformance runs canonical contract cases against a target's runtime through its JSON Lines harness process.
contracts
Package contracts loads, validates, and normalizes the canonical runtime contract catalog and the target mappings that implement it.
Package contracts loads, validates, and normalizes the canonical runtime contract catalog and the target mappings that implement it.
driver
Package driver runs the compiler pipeline: load, validate, lower, emit.
Package driver runs the compiler pipeline: load, validate, lower, emit.
emit/artifact
Package artifact describes native source files and source-package ownership for one whole-program emission.
Package artifact describes native source files and source-package ownership for one whole-program emission.
emit/c
Package c emits C17.
Package c emits C17.
emit/csharp
Package csharp emits C# 12 for .NET 8.
Package csharp emits C# 12 for .NET 8.
emit/golang
Package golang emits lowered Go: a Go program whose control flow is structured from the IR's basic blocks and whose implementation-dependent operations (append growth, map order, panic reporting) call the Goalchemy Go runtime.
Package golang emits lowered Go: a Go program whose control flow is structured from the IR's basic blocks and whose implementation-dependent operations (append growth, map order, panic reporting) call the Goalchemy Go runtime.
emit/java
Package java emits Java 21.
Package java emits Java 21.
emit/py
Package py emits Python 3.10+.
Package py emits Python 3.10+.
emit/rust
Package rust emits Rust (edition 2021).
Package rust emits Rust (edition 2021).
emit/swift
Package swift emits native Swift with explicit Go storage.
Package swift emits native Swift with explicit Go storage.
emit/ts
Package ts emits TypeScript for Node.js.
Package ts emits TypeScript for Node.js.
frontend
Package frontend loads Go packages under the locked Goalchemy source profile.
Package frontend loads Go packages under the locked Goalchemy source profile.
hardening
Package hardening generates small, type-valid programs for differential testing.
Package hardening generates small, type-valid programs for differential testing.
ir
Package ir defines Goalchemy's target-independent typed intermediate representation: typed functions made of basic blocks with explicit branch targets, local storage cells, and three-address instructions whose evaluation order and copy boundaries are already decided by lowering.
Package ir defines Goalchemy's target-independent typed intermediate representation: typed functions made of basic blocks with explicit branch targets, local storage cells, and three-address instructions whose evaluation order and copy boundaries are already decided by lowering.
jsontype
Package jsontype describes Go types for std/encoding/json the way encoding/json's reflection does, so the language gate and the lowering of json.Marshal agree on encoders, struct fields and unsupported types.
Package jsontype describes Go types for std/encoding/json the way encoding/json's reflection does, so the language gate and the lowering of json.Marshal agree on encoders, struct fields and unsupported types.
link
Package link resolves the runtime files a generated program needs and writes them with a build manifest.
Package link resolves the runtime files a generated program needs and writes them with a build manifest.
lower
Package lower translates type-checked Goalchemy source into IR.
Package lower translates type-checked Goalchemy source into IR.
naming
Package naming assigns deterministic private identifiers to emitted IR.
Package naming assigns deterministic private identifiers to emitted IR.
project
Package project reads goalchemy.yaml, the project configuration that records roots, build tags, the language gate, and target outputs.
Package project reads goalchemy.yaml, the project configuration that records roots, build tags, the language gate, and target outputs.
protoc
Package protoc generates Goalchemy-subset Go from protobuf descriptors: one struct per message with proto3 JSON encoding through std/encoding/protojson.
Package protoc generates Goalchemy-subset Go from protobuf descriptors: one struct per message with proto3 JSON encoding through std/encoding/protojson.
sourcemap
Package sourcemap writes Source Map v3 files mapping generated lines back to Goalchemy source positions.
Package sourcemap writes Source Map v3 files mapping generated lines back to Goalchemy source positions.
specgen
Package specgen generates per-target specifications, harness bindings and the runtime library reference from the canonical contract catalog.
Package specgen generates per-target specifications, harness bindings and the runtime library reference from the canonical contract catalog.
structure
Package structure rebuilds structured control flow (if, loops, labelled blocks, break and continue) from a function's block graph, so targets print source-like code instead of a block-dispatch loop.
Package structure rebuilds structured control flow (if, loops, labelled blocks, break and continue) from a function's block graph, so targets print source-like code instead of a block-dispatch loop.
subset
Package subset validates that type-checked Go source stays within the implemented Goalchemy feature gates.
Package subset validates that type-checked Go source stays within the implemented Goalchemy feature gates.
testutil
Package testutil runs language fixtures natively and through Goalchemy targets and compares their observations.
Package testutil runs language fixtures natively and through Goalchemy targets and compares their observations.
lib
callback
Package callback declares a bounded asynchronous host callback capability.
Package callback declares a bounded asynchronous host callback capability.
checksum
Package checksum provides non-cryptographic checksums backed by host APIs where available.
Package checksum provides non-cryptographic checksums backed by host APIs where available.
clock
SPDX-License-Identifier: Apache-2.0 Package clock provides wall time, independent of Goalchemy virtual time.
SPDX-License-Identifier: Apache-2.0 Package clock provides wall time, independent of Goalchemy virtual time.
context
Package context provides Goalchemy's cancellation contexts; deadlines use the virtual clock.
Package context provides Goalchemy's cancellation contexts; deadlines use the virtual clock.
crypto
SPDX-License-Identifier: Apache-2.0 Package crypto provides bounded native cryptographic capabilities.
SPDX-License-Identifier: Apache-2.0 Package crypto provides bounded native cryptographic capabilities.
encoding
SPDX-License-Identifier: Apache-2.0 Package encoding provides canonical RFC 4648 base64 encodings.
SPDX-License-Identifier: Apache-2.0 Package encoding provides canonical RFC 4648 base64 encodings.
errors
Package errors provides Goalchemy's error values.
Package errors provides Goalchemy's error values.
http
SPDX-License-Identifier: Apache-2.0 Package http provides bounded HTTP exchanges, retaining TLS verification and returning redirect responses without following them.
SPDX-License-Identifier: Apache-2.0 Package http provides bounded HTTP exchanges, retaining TLS verification and returning redirect responses without following them.
json
Package json is the native Go implementation behind std/encoding/json used when the program runs with the Go toolchain.
Package json is the native Go implementation behind std/encoding/json used when the program runs with the Go toolchain.
log
Package log routes structured log records from Goalchemy programs to a sink the host application installs.
Package log routes structured log records from Goalchemy programs to a sink the host application installs.
os
Package os reads and writes whole host files and opens files as handles.
Package os reads and writes whole host files and opens files as handles.
runtime
Package runtime provides Goalchemy's scheduler controls.
Package runtime provides Goalchemy's scheduler controls.
sync
Package sync provides Goalchemy's synchronization primitives.
Package sync provides Goalchemy's synchronization primitives.
task
Package task provides structured waiting for Goalchemy programs.
Package task provides structured waiting for Goalchemy programs.
time
Package time provides Goalchemy's virtual clock: Sleep advances it deterministically.
Package time provides Goalchemy's virtual clock: Sleep advances it deterministically.
std
bytes
Package bytes manipulates byte slices.
Package bytes manipulates byte slices.
connect
Package connect is a Connect protocol client for unary calls with the JSON codec, the transport behind services generated by protoc-gen-goalchemy.
Package connect is a Connect protocol client for unary calls with the JSON codec, the transport behind services generated by protoc-gen-goalchemy.
encoding/binary
Package binary translates between unsigned integers and byte sequences, and encodes varints.
Package binary translates between unsigned integers and byte sequences, and encodes varints.
encoding/hex
Package hex encodes and decodes hexadecimal strings.
Package hex encodes and decodes hexadecimal strings.
encoding/json
Package json encodes and decodes JSON with the semantics of Go's encoding/json.
Package json encodes and decodes JSON with the semantics of Go's encoding/json.
encoding/json/jcs
Package jcs implements the JSON Canonicalization Scheme of RFC 8785.
Package jcs implements the JSON Canonicalization Scheme of RFC 8785.
encoding/jsonvalue
Package jsonvalue parses, inspects, builds and encodes JSON documents of any shape as a tree of Values, without declaring Go types.
Package jsonvalue parses, inspects, builds and encodes JSON documents of any shape as a tree of Values, without declaring Go types.
encoding/protojson
Package protojson encodes and decodes proto3 JSON for messages generated by protoc-gen-goalchemy, matching google.golang.org/protobuf/encoding/protojson.
Package protojson encodes and decodes proto3 JSON for messages generated by protoc-gen-goalchemy, matching google.golang.org/protobuf/encoding/protojson.
errors
Package errors implements functions to manipulate errors: creation, wrapping inspection with Is and As, and joining.
Package errors implements functions to manipulate errors: creation, wrapping inspection with Is and As, and joining.
fmt
Package fmt formats values into strings and errors with Go's verbs, flags, widths, precisions and argument indexes.
Package fmt formats values into strings and errors with Go's verbs, flags, widths, precisions and argument indexes.
http
Package http sends HTTP requests whose bodies stream through std/io: the request body is read from an io.Reader and the response body is an io.ReadCloser, so neither is held in memory whole.
Package http sends HTTP requests whose bodies stream through std/io: the request body is read from an io.Reader and the response body is an io.ReadCloser, so neither is held in memory whole.
io
Package io provides basic interfaces to I/O primitives and helpers that combine them, matching Go's io package.
Package io provides basic interfaces to I/O primitives and helpers that combine them, matching Go's io package.
io/hostio
Package hostio adapts host streams passed to library exports as std/io interfaces.
Package hostio adapts host streams passed to library exports as std/io interfaces.
log/slog
Package slog provides structured logging with Go's log/slog API.
Package slog provides structured logging with Go's log/slog API.
net
Package net provides the host and port helpers of Go's net package: JoinHostPort, SplitHostPort and AddrError.
Package net provides the host and port helpers of Go's net package: JoinHostPort, SplitHostPort and AddrError.
net/url
Package url parses URLs and implements query escaping, following Go's net/url: the same parsing rules, escaping and error messages.
Package url parses URLs and implements query escaping, following Go's net/url: the same parsing rules, escaping and error messages.
os
Package os reads and writes whole files on the host file system.
Package os reads and writes whole files on the host file system.
sort
Package sort sorts collections through the Len, Less, and Swap methods.
Package sort sorts collections through the Len, Less, and Swap methods.
strconv
Package strconv converts between integers, Booleans, and their string forms, and formats floating-point numbers.
Package strconv converts between integers, Booleans, and their string forms, and formats floating-point numbers.
strings
Package strings manipulates UTF-8 encoded strings.
Package strings manipulates UTF-8 encoded strings.
time
Package time measures and displays time.
Package time measures and displays time.
unicode
Package unicode classifies runes and maps their case using the Unicode tables of the reference Go toolchain.
Package unicode classifies runes and maps their case using the Unicode tables of the reference Go toolchain.
unicode/utf8
Package utf8 encodes and decodes UTF-8 text.
Package utf8 encodes and decodes UTF-8 text.
targets
go/runtime
Package rt is the Goalchemy runtime for the lowered Go target.
Package rt is the Goalchemy runtime for the lowered Go target.
go/tests/harness command
Command harness serves runtime conformance requests for the Go target over JSON Lines on standard input and output.
Command harness serves runtime conformance requests for the Go target over JSON Lines on standard input and output.
tests
corpus/r008_defer_named_struct_result command
goalchemy:gate cooperative
goalchemy:gate cooperative
corpus/r009_range_empty_struct_map command
goalchemy:gate cooperative
goalchemy:gate cooperative

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL