Status: early development. Core architecture is settled and the first reference plugins (require, assert) are landing. This is a work-in-progress published openly to share the design.
go-sugar is a source-to-source transformation toolchain that extends Go with syntactic sugar. You write .gos files -
a superset of Go with sugar constructs - and go-sugar compiles them down to plain, idiomatic .go that the standard
toolchain compiles, formats, and lints without modification.
The goal is to cut boilerplate without leaving the Go ecosystem. The near-term focus is test code, where boilerplate
is densest. The longer-term vision includes a plugin protocol, LSP integration, and eventually defensive-programming
constructs usable in production (think C's assert.h with NDEBUG).
The first two reference sugar plugins, require and assert, target the most repetitive parts of Go tests.
test file with require sugar:
// test.gos
package demo
import "testing"
func Test_Something(t *testing.T) {
require setUp()
svc := require NewService() "cannot make Service"
// ...
}
will generate test.gos.go:
package demo
import "testing"
func Test_Something(t *testing.T) {
if err := setUp(); err != nil {
t.Fatalf("%s: %v", "setUp()", err)
}
svc, err := NewService()
if err != nil {
t.Fatal("cannot make Service")
}
// ...
}test file with assert sugar:
// test.gos
package demo
import "testing"
func Test_Something(t *testing.T) {
// ...
assert(age > 18)
assert(username == "test")
assert(result == expected)
assert(a == "a" || !b)
assert(called != 0, "service is not called")
assert(returned == 0, "program exits with error code %v", returned)
}will generate test.gos.go:
package demo
import "testing"
func Test_Something(t *testing.T) {
// ...
if !(age > 18) {
t.Errorf("%s\n\t got: %#v", "age > 18", age)
}
if !(username == "test") {
t.Errorf("%s\n\t got: %#v", "username == \"test\"", username)
}
if !(result == expected) {
t.Errorf("%s\n\t got: %#v\n\twant: %#v", "result == expected", result, expected)
}
if !(a == "a" || !b) {
t.Error("assertion failed: a == \"a\" || !b")
}
if !(called != 0) {
t.Error("service is not called")
}
if !(returned == 0) {
t.Errorf("program exits with error code %v", returned)
}
}- Infrastructure:
- Lexical parser helper
- Golden tests (use .md as virtual file system)
- Sugar Plugin API
- SDK and transport to independent sugar binary
- Backend - in progress
- Generate pipeline with watch
- Format pipeline with watch
- Built-in sugars - in progress
- require
- assert - in progress
- Test pipeline with watch and sourcemap rewrite - in progress
- LSP proxying
goplswith sourcemap rewrite
- Frontend:
- LSP and example configs for nvim
- vscode extension
- IDEA plugin
- lexeme-viewer: wasm tool to view lexeme in browser, use port 39800
- railroad-diagram: tool to generate railroad diagram svg, use port 39801 (thanks to tabatkins/railroad-diagrams)
PRs are welcome! Distributed under the Apache License 2.0.