Skip to main content

Command Palette

Search for a command to run...

Playground & Getting Started with Golo 🤓

Published
4 min read

This post is a follow-up to "I Resurrected the Language That Boosted My "Geek Speaker" Career: Golo is Back!"

Quick reminder: what is Golo?

Golo was originally a dynamic language running on the JVM... The project was archived a few years ago. I decided to resurrect it by porting it to Go as an interpreter and calling it GoloScript, with the goal of keeping as much of the "spirit of Golo" as possible.

I haven't made an official release yet, but the project is coming along nicely. So for the curious, I've created a "little" playground to discover GoloScript.

This also allows me to re-test one by one the features implemented by Claude, who did a hell of a job, but also took some liberties with certain things 😂.

So what about this playground?

You'll find it here: https://codeberg.org/TypeUnsafe/golo-playground

But why Codeberg? Because I like the idea of an "alternative" code hosting... Like Golo. You could even say that GoloScript is a sovereign language 🤴🏻.

To install it, clone the repo:

git clone https://codeberg.org/TypeUnsafe/golo-playground.git
cd golo-playground

To launch it:

docker compose up -d

To use it:

docker compose exec golo-playground bash

And you'll land on the following prompt:

You can verify that GoloScript works by running:

golo --version
golo --about

You can run your first and essential "Hello World" in GoloScript:

cd 01-hello-world
golo hello.golo

You can even edit the code, the Micro editor is installed in the playground and historically had syntax highlighting support for Golo:

micro hello.golo

I encourage you to try the other examples in the playground/ directory:

02 - Variables and Constants

module main

function main = |args| {

  var name = "Golo"
  println("Hello, " + name + "!")
  name = "GoloScript"
  println("Welcome to " + name + ".")

  let pi = 3.14159
  println("The value of pi is approximately " + str(pi) + ".")

  try {
    pi = 3.14  # This will cause an error because 'pi' is immutable
    println("Updated value of pi: " + str(pi) + ".")
  } catch (e) {
    println("Error: " + e: message())
  }
}

The rules of the game:

var - For mutable variables

var counter = 0
counter = counter + 1  # ✅ Ok

Use var when you really need to modify the value.

let - For constants

let pi = 3.14159
pi = 3.14  # ❌ Error!

Use let by default. It's safer, cleaner, more functional.

03 - Functions and Closures

module main

# Function
function say_hello = |name| {
  return "Hello, " + name + "!"
}

function main = |args| {

  println(say_hello("Golo"))

  # Closures
  let double = |x| -> x * 2
  let triple = |x| -> x * 3

  println("Double 5:", double(5))
  println("Triple 5:", triple(5))

  # List of closures
  let operations = list(
    |x| -> x + 1,
    |x| -> x * 2,
    |x| -> x - 5
  )

  let value = 10
  println("Starting value:", value)
  println("After +1:", operations: get(0)(value))
  println("After *2:", operations: get(1)(value))
  println("After -5:", operations: get(2)(value))
}

Closure syntax:

Simple expression:

let add = |a, b| -> a + b

Multiple statements:

let complex = |x| {
  let result = x * 2
  return result + 1
}

Without parameters:

let greet = -> "Hello!"
let random = -> 42

Method calls:

In Golo, we use : to call methods:

operations: get(0)      # get item at index 0

04 - Imports and Modules (code organization)

Structure:

04-imports/
├── main.golo
└── my_tools/
    ├── hello.golo
    ├── hey.golo
    └── utils/
        └── plop.golo

Golden rule: The module name MUST match the file path.

my_tools/hello.golo

module my_tools.hello

function say_hello = |name| {
  return "Hello, " + name + "!"
}

The my_tools.hello module corresponds to the my_tools/hello.golo file.

my_tools/utils/plop.golo

module my_tools.utils.plop

function plop = {
  return "Plop!"
}

The my_tools.utils.plop module corresponds to the my_tools/utils/plop.golo file.

main.golo

module main

import my_tools.hello
import my_tools.hey
import my_tools.utils.plop

function main = |args| {
  println(say_hello("Golo"))
  println(say_hey("Golo"))
  println(plop())
}

Conclusion

Golo is simple, expressive, and gets the job done. No unnecessary complications, just what you need to write clean code.

Now, it's your turn to play.

Happy coding! 🤓