DEV Community

Alex Spinov
Alex Spinov

Posted on

Borgo Has a Free API: Rust-Like Syntax That Compiles to Go

Borgo is a new language with Rust-like syntax — algebraic types, pattern matching, Result/Option — that compiles directly to Go. You get Rust ergonomics with Go's ecosystem and deployment story.

Why Borgo Matters

Go is simple but lacks enums, pattern matching, and proper error types. Rust has them all but is complex. Borgo gives you Rust's type system features while compiling to readable Go code.

What you get for free:

  • Algebraic data types (tagged unions/enums)
  • Pattern matching with exhaustiveness checking
  • Result and Option types (no nil panics)
  • Compiles to clean, readable Go code
  • Use any Go library directly
  • Go-speed performance (it IS Go under the hood)

Quick Start

# Install
go install github.com/nicholasgasior/borgo@latest

# Create project
borgo init my-project
cd my-project

# Build (generates Go code)
borgo build

# Run
go run .
Enter fullscreen mode Exit fullscreen mode

Enums and Pattern Matching

use fmt

enum Shape {
    Circle(float64),
    Rectangle(float64, float64),
    Triangle(float64, float64, float64),
}

fn area(s: Shape) -> float64 {
    match s {
        Shape.Circle(r) => 3.14159 * r * r,
        Shape.Rectangle(w, h) => w * h,
        Shape.Triangle(a, b, c) => {
            let s = (a + b + c) / 2.0
            math.Sqrt(s * (s-a) * (s-b) * (s-c))
        }
    }
}

fn main() {
    let shapes = [Shape.Circle(5.0), Shape.Rectangle(3.0, 4.0)]
    for s in shapes {
        fmt.Printf("Area: %.2f\n", area(s))
    }
}
Enter fullscreen mode Exit fullscreen mode

Result Type (No nil Panics)

use fmt
use strconv

enum Result<T, E> {
    Ok(T),
    Err(E),
}

enum ParseError {
    InvalidFormat(string),
    OutOfRange(int),
}

fn parse_age(input: string) -> Result<int, ParseError> {
    match strconv.Atoi(input) {
        (age, nil) if age > 0 && age < 150 => Result.Ok(age),
        (age, nil) => Result.Err(ParseError.OutOfRange(age)),
        (_, err) => Result.Err(ParseError.InvalidFormat(err.Error())),
    }
}

fn main() {
    match parse_age("25") {
        Result.Ok(age) => fmt.Printf("Age: %d\n", age),
        Result.Err(ParseError.InvalidFormat(msg)) => fmt.Printf("Bad format: %s\n", msg),
        Result.Err(ParseError.OutOfRange(n)) => fmt.Printf("Out of range: %d\n", n),
    }
}
Enter fullscreen mode Exit fullscreen mode

Option Type

enum Option<T> {
    Some(T),
    None,
}

struct User {
    name: string,
    email: Option<string>,
    age: Option<int>,
}

fn display_email(user: User) -> string {
    match user.email {
        Option.Some(email) => email,
        Option.None => "no email provided",
    }
}
Enter fullscreen mode Exit fullscreen mode

Using Go Libraries

use net/http
use encoding/json
use fmt

struct ApiResponse {
    message: string,
    status: int,
}

fn handler(w: http.ResponseWriter, r: *http.Request) {
    let response = ApiResponse {
        message: "Hello from Borgo!",
        status: 200,
    }
    let data, _ = json.Marshal(response)
    w.Header().Set("Content-Type", "application/json")
    w.Write(data)
}

fn main() {
    http.HandleFunc("/", handler)
    fmt.Println("Server on :8080")
    http.ListenAndServe(":8080", nil)
}
Enter fullscreen mode Exit fullscreen mode

Generated Go Code

Borgo compiles to clean, idiomatic Go:

// Generated from Borgo enum
type Shape interface {
    isShape()
}

type ShapeCircle struct {
    Value float64
}
func (ShapeCircle) isShape() {}

type ShapeRectangle struct {
    Width, Height float64
}
func (ShapeRectangle) isShape() {}
Enter fullscreen mode Exit fullscreen mode

Useful Links


Building Go-powered data tools? Check out my developer tools on Apify for ready-made web scrapers, or email spinov001@gmail.com for custom solutions.

Top comments (0)