Borgo is a statically typed language that compiles to Go

https://github.com/borgo-lang/borgo

The Borgo Programming Language

Borgo sits between Go and Rust


build

I want a language for writing applications that is more expressive than Go but less complex than Rust.

Go is simple and straightforward, but I often wish it offered more type safety. Rust is very nice to work with (at least for single threaded code) but it's too broad and complex, sometimes painfully so.

Borgo is a new language that transpiles to Go. It's fully compatible with existing Go packages.

Borgo syntax is similar to Rust, with optional semi-colons.

Tutorial

Check out the online playground for a tour of the language.

You can also take a look at test files for working Borgo code:

Features

Algebraic data types and pattern matching

use fmt
enum NetworkState {
    Loading,
    Failed(int),
    Success(string),
}
let msg = match state {
    NetworkState.Loading => "still loading",
    NetworkState.Failed(code) => fmt.Sprintf("Got error code: %d", code),
    NetworkState.Success(res) => res,
}

Option<T> instead of nil

// import packages from Go stdlib
use fmt
use os
let key = os.LookupEnv("HOME")
match key {
    Some(s) => fmt.Println("home dir:", s),
    None => fmt.Println("Not found in env"),
}

Result<T, E> instead of multiple return values

use fmt
use net.http
fn makeRequest() -> Result<int, error> {
    let request = http.Get("http://example.com")
    match request {
        Ok(resp) => Ok(resp.StatusCode),
        Err(err) => Err(fmt.Errorf("failed http request %w", err))
    }
}

Error handling with ? operator

use fmt
use io
use os
fn copyFile(src: string, dst: string) -> Result<(), error> {
    let stat = os.Stat(src)?
    if !stat.Mode().IsRegular() {
        return Err(fmt.Errorf("%s is not a regular file", src))
    }
    let source = os.Open(src)?
    defer source.Close()
    let destination = os.Create(dst)?
    defer destination.Close()
    // ignore number of bytes copied
    let _ = io.Copy(destination, source)?
    Ok(())
}

Guessing game example

Small game from the Rust book, implemented in Borgo.

Things to note:

  • import packages from Go stdlib
  • strconv.Atoi returns an Option<int>
  • Reader.ReadString returns a Result<string, error> (which can be unwrapped)
use bufio
use fmt
use math.rand
use os
use strconv
use strings
fn main() {
    let reader = bufio.NewReader(os.Stdin)
    let secret = rand.Intn(100) + 1
    loop {
        fmt.Println("Please input your guess.")
        let text = reader.ReadString('\n').Unwrap()
        let text = strings.TrimSpace(text)
        let guess = match strconv.Atoi(text) {
            Ok(n) => n,
            Err(_) => continue,
        }
        fmt.Println("You guessed: ", guess)
        if guess < secret {
            fmt.Println("Too small!")
        } else if guess > secret {
            fmt.Println("Too big!")
        } else {
            fmt.Println("Correct!")
            break
        }
    }
}

Running locally

Borgo is written in Rust, so you'll need cargo.

To compile all .brg files in the current folder:

The compiler will generate .go files, which you can run as normal:

# generate a go.mod file if needed
# $ go mod init foo
$ go run .
{
"by": "manx",
"descendants": 545,
"id": 40211891,
"kids": [
40218563,
40212796,
40218648,
40220218,
40212961,
40214490,
40213689,
40212665,
40213108,
40214599,
40218686,
40212770,
40213418,
40219688,
40212499,
40212684,
40212707,
40212673,
40224037,
40261525,
40220880,
40221189,
40215525,
40230002,
40213573,
40212777,
40215382,
40233237,
40213054,
40218983,
40215228,
40229949,
40217542,
40213571,
40212585,
40220863,
40212525,
40217288,
40221346,
40213230,
40213234,
40222609,
40212828
],
"score": 666,
"time": 1714490002,
"title": "Borgo is a statically typed language that compiles to Go",
"type": "story",
"url": "https://github.com/borgo-lang/borgo"
}
{
"author": "borgo-lang",
"date": null,
"description": "Borgo is a statically typed language that compiles to Go. - borgo-lang/borgo",
"image": "https://opengraph.githubassets.com/66839f3546705fae56ae966a2de810dc4a5935970cd345a2f67794007efc2da8/borgo-lang/borgo",
"logo": "https://logo.clearbit.com/github.com",
"publisher": "GitHub",
"title": "GitHub - borgo-lang/borgo: Borgo is a statically typed language that compiles to Go.",
"url": "https://github.com/borgo-lang/borgo"
}
{
"url": "https://github.com/borgo-lang/borgo",
"title": "GitHub - borgo-lang/borgo: Borgo is a statically typed language that compiles to Go.",
"description": "The Borgo Programming Language I want a language for writing applications that is more expressive than Go but less complex than Rust. Go is simple and straightforward, but I often wish it offered more type...",
"links": [
"https://github.com/borgo-lang/borgo"
],
"image": "https://opengraph.githubassets.com/66839f3546705fae56ae966a2de810dc4a5935970cd345a2f67794007efc2da8/borgo-lang/borgo",
"content": "<div><article><p></p><h2>The Borgo Programming Language</h2><a target=\"_blank\" href=\"https://github.com/borgo-lang/borgo#the-borgo-programming-language\"></a><p></p>\n<p><a target=\"_blank\" href=\"https://raw.githubusercontent.com/borgo-lang/borgo-lang.github.io/main/borgo.jpg\"><img src=\"https://raw.githubusercontent.com/borgo-lang/borgo-lang.github.io/main/borgo.jpg\" alt=\"Borgo sits between Go and Rust\" /></a></p>\n<hr />\n<p><a target=\"_blank\" href=\"https://github.com/borgo-lang/borgo/actions/workflows/ci.yml/badge.svg\"><img src=\"https://github.com/borgo-lang/borgo/actions/workflows/ci.yml/badge.svg\" alt=\"build\" /></a></p>\n<p>I want a language for writing applications that is more expressive than Go but\nless complex than Rust.</p>\n<p>Go is simple and straightforward, but I often wish it offered more type safety.\nRust is very nice to work with (at least for single threaded code) but it's too\nbroad and complex, sometimes painfully so.</p>\n<p><strong>Borgo is a new language that transpiles to Go</strong>. It's fully compatible with\nexisting Go packages.</p>\n<p>Borgo syntax is similar to Rust, with optional semi-colons.</p>\n<p></p><h2>Tutorial</h2><a target=\"_blank\" href=\"https://github.com/borgo-lang/borgo#tutorial\"></a><p></p>\n<p>Check out the <strong><a target=\"_blank\" href=\"https://borgo-lang.github.io/\">online playground</a></strong> for a tour\nof the language.</p>\n<p>You can also take a look at test files for working Borgo code:</p>\n<ul>\n<li><a target=\"_blank\" href=\"https://github.com/borgo-lang/borgo/blob/main/compiler/test/codegen-emit.md\">codegen-emit.md</a></li>\n<li><a target=\"_blank\" href=\"https://github.com/borgo-lang/borgo/blob/main/compiler/test/infer-expr.md\">infer-expr.md</a></li>\n<li><a target=\"_blank\" href=\"https://github.com/borgo-lang/borgo/blob/main/compiler/test/infer-file.md\">infer-file.md</a></li>\n</ul>\n<p></p><h2>Features</h2><a target=\"_blank\" href=\"https://github.com/borgo-lang/borgo#features\"></a><p></p>\n<p></p><h2>Algebraic data types and pattern matching</h2><a target=\"_blank\" href=\"https://github.com/borgo-lang/borgo#algebraic-data-types-and-pattern-matching\"></a><p></p>\n<div><pre><span>use</span> fmt<span></span>\n<span>enum</span> <span>NetworkState</span> <span>{</span>\n <span>Loading</span><span>,</span>\n <span>Failed</span><span>(</span><span>int</span><span>)</span><span>,</span>\n <span>Success</span><span>(</span><span>string</span><span>)</span><span>,</span>\n<span>}</span>\n<span>let</span> msg = <span>match</span> state <span>{</span>\n <span>NetworkState</span><span>.</span><span>Loading</span> =&gt; <span>\"still loading\"</span><span>,</span>\n <span>NetworkState</span><span>.</span><span>Failed</span><span>(</span>code<span>)</span> =&gt; fmt<span>.</span><span>Sprintf</span><span>(</span><span>\"Got error code: %d\"</span><span>,</span> code<span>)</span><span>,</span>\n <span>NetworkState</span><span>.</span><span>Success</span><span>(</span>res<span>)</span> =&gt; res<span>,</span>\n<span>}</span><span></span></pre></div>\n<hr />\n<p></p><h2><code>Option&lt;T&gt;</code> instead of <code>nil</code></h2><a target=\"_blank\" href=\"https://github.com/borgo-lang/borgo#optiont-instead-of-nil\"></a><p></p>\n<div><pre><span>// import packages from Go stdlib</span>\n<span>use</span> fmt<span></span>\n<span>use</span> os<span></span>\n<span>let</span> key = os<span>.</span><span>LookupEnv</span><span>(</span><span>\"HOME\"</span><span>)</span><span></span>\n<span>match</span> key <span>{</span>\n <span>Some</span><span>(</span>s<span>)</span> =&gt; fmt<span>.</span><span>Println</span><span>(</span><span>\"home dir:\"</span><span>,</span> s<span>)</span><span>,</span>\n <span>None</span> =&gt; fmt<span>.</span><span>Println</span><span>(</span><span>\"Not found in env\"</span><span>)</span><span>,</span>\n<span>}</span></pre></div>\n<hr />\n<p></p><h2><code>Result&lt;T, E&gt;</code> instead of multiple return values</h2><a target=\"_blank\" href=\"https://github.com/borgo-lang/borgo#resultt-e-instead-of-multiple-return-values\"></a><p></p>\n<div><pre><span>use</span> fmt<span></span>\n<span>use</span> net<span>.</span>http<span></span>\n<span>fn</span> <span>makeRequest</span><span>(</span><span>)</span> -&gt; <span>Result</span><span>&lt;</span><span>int</span><span>,</span> <span>error</span><span>&gt;</span> <span>{</span>\n <span>let</span> request = http<span>.</span><span>Get</span><span>(</span><span>\"http://example.com\"</span><span>)</span><span></span>\n <span>match</span> request <span>{</span>\n <span>Ok</span><span>(</span>resp<span>)</span> =&gt; <span>Ok</span><span>(</span>resp<span>.</span><span>StatusCode</span><span>)</span><span>,</span>\n <span>Err</span><span>(</span>err<span>)</span> =&gt; <span>Err</span><span>(</span>fmt<span>.</span><span>Errorf</span><span>(</span><span>\"failed http request %w\"</span><span>,</span> err<span>)</span><span>)</span>\n <span>}</span>\n<span>}</span></pre></div>\n<hr />\n<p></p><h2>Error handling with <code>?</code> operator</h2><a target=\"_blank\" href=\"https://github.com/borgo-lang/borgo#error-handling-with--operator\"></a><p></p>\n<div><pre><span>use</span> fmt<span></span>\n<span>use</span> io<span></span>\n<span>use</span> os<span></span>\n<span>fn</span> <span>copyFile</span><span>(</span><span>src</span><span>:</span> <span>string</span><span>,</span> <span>dst</span><span>:</span> <span>string</span><span>)</span> -&gt; <span>Result</span><span>&lt;</span><span>(</span><span>)</span><span>,</span> <span>error</span><span>&gt;</span> <span>{</span>\n <span>let</span> stat = os<span>.</span><span>Stat</span><span>(</span>src<span>)</span>?<span></span>\n <span>if</span> !stat<span>.</span><span>Mode</span><span>(</span><span>)</span><span>.</span><span>IsRegular</span><span>(</span><span>)</span> <span>{</span>\n <span>return</span> <span>Err</span><span>(</span>fmt<span>.</span><span>Errorf</span><span>(</span><span>\"%s is not a regular file\"</span><span>,</span> src<span>)</span><span>)</span>\n <span>}</span>\n <span>let</span> source = os<span>.</span><span>Open</span><span>(</span>src<span>)</span>?\n defer source<span>.</span><span>Close</span><span>(</span><span>)</span><span></span>\n <span>let</span> destination = os<span>.</span><span>Create</span><span>(</span>dst<span>)</span>?\n defer destination<span>.</span><span>Close</span><span>(</span><span>)</span>\n <span>// ignore number of bytes copied</span><span></span>\n <span>let</span> _ = io<span>.</span><span>Copy</span><span>(</span>destination<span>,</span> source<span>)</span>?<span></span>\n <span>Ok</span><span>(</span><span>(</span><span>)</span><span>)</span>\n<span>}</span></pre></div>\n<hr />\n<p></p><h2>Guessing game example</h2><a target=\"_blank\" href=\"https://github.com/borgo-lang/borgo#guessing-game-example\"></a><p></p>\n<p>Small game from the Rust book, implemented in Borgo.</p>\n<p>Things to note:</p>\n<ul>\n<li>import packages from Go stdlib</li>\n<li><code>strconv.Atoi</code> returns an <code>Option&lt;int&gt;</code></li>\n<li><code>Reader.ReadString</code> returns a <code>Result&lt;string, error&gt;</code> (which can be unwrapped)</li>\n</ul>\n<div><pre><span>use</span> bufio<span></span>\n<span>use</span> fmt<span></span>\n<span>use</span> math<span>.</span>rand\n<span>use</span> os<span></span>\n<span>use</span> strconv<span></span>\n<span>use</span> strings<span></span>\n<span>fn</span> <span>main</span><span>(</span><span>)</span> <span>{</span>\n <span>let</span> reader = bufio<span>.</span><span>NewReader</span><span>(</span>os<span>.</span><span>Stdin</span><span>)</span><span></span>\n <span>let</span> secret = rand<span>.</span><span>Intn</span><span>(</span><span>100</span><span>)</span> + <span>1</span>\n <span>loop</span> <span>{</span>\n fmt<span>.</span><span>Println</span><span>(</span><span>\"Please input your guess.\"</span><span>)</span><span></span>\n <span>let</span> text = reader<span>.</span><span>ReadString</span><span>(</span><span>'\\n'</span><span>)</span><span>.</span><span>Unwrap</span><span>(</span><span>)</span><span></span>\n <span>let</span> text = strings<span>.</span><span>TrimSpace</span><span>(</span>text<span>)</span><span></span>\n <span>let</span> guess = <span>match</span> strconv<span>.</span><span>Atoi</span><span>(</span>text<span>)</span> <span>{</span>\n <span>Ok</span><span>(</span>n<span>)</span> =&gt; n<span>,</span>\n <span>Err</span><span>(</span>_<span>)</span> =&gt; <span>continue</span><span>,</span>\n <span>}</span>\n fmt<span>.</span><span>Println</span><span>(</span><span>\"You guessed: \"</span><span>,</span> guess<span>)</span><span></span>\n <span>if</span> guess &lt; secret <span>{</span>\n fmt<span>.</span><span>Println</span><span>(</span><span>\"Too small!\"</span><span>)</span>\n <span>}</span> <span>else</span> <span>if</span> guess &gt; secret <span>{</span>\n fmt<span>.</span><span>Println</span><span>(</span><span>\"Too big!\"</span><span>)</span>\n <span>}</span> <span>else</span> <span>{</span>\n fmt<span>.</span><span>Println</span><span>(</span><span>\"Correct!\"</span><span>)</span>\n break\n <span>}</span>\n <span>}</span><span></span>\n<span>}</span></pre></div>\n<p></p><h2>Running locally</h2><a target=\"_blank\" href=\"https://github.com/borgo-lang/borgo#running-locally\"></a><p></p>\n<p>Borgo is written in Rust, so you'll need <code>cargo</code>.</p>\n<p>To compile all <code>.brg</code> files in the current folder:</p>\n<p>The compiler will generate <code>.go</code> files, which you can run as normal:</p>\n<div><pre><span><span>#</span> generate a go.mod file if needed</span>\n<span><span>#</span> $ go mod init foo</span>\n$ go run <span>.</span></pre></div>\n</article></div>",
"author": "",
"favicon": "https://github.githubassets.com/favicons/favicon.svg",
"source": "github.com",
"published": "",
"ttr": 88,
"type": "object"
}