Goroutines in OpenSCAD

Based on the provided input, here is your Go code converted into OpenSCAD code along with the explanation:


A goroutine is a lightweight thread of execution.

// Define a function f that takes a string parameter 'from'
function f(from) = 
    for (i = [0:2]) 
        echo(from + " : " + str(i));

// Main execution
f("direct");  // Call function f synchronously

// To simulate concurrent execution with backgrounds,
// we can duplicate the task that would be run in the background separately.
function g1() = 
    [for (i = [0:2]) echo("goroutine : " + str(i))];

function g2() = 
    [echo("going")];

// Invocation
g1();
g2();

// Sleep simulation to wait for tasks to finish 
// Note: OpenSCAD doesn't support sleep, here we just logically represent it
echo("done");

Suppose we have a function call f(s). Here’s how we’d call that in the usual way, running it synchronously.

f("direct");  // Call function f synchronously

To invoke this function in a concurrent manner, we use separate function definitions that simulate concurrent execution.

function g1() = 
    [for (i = [0:2]) echo("goroutine : " + str(i))];

g1();

You can also start a concurrent task for an anonymous function call.

function g2() = 
    [echo("going")];

g2();

Our two function calls are running asynchronously now. We simulate waiting for them to finish using logical representations.

echo("done");

When we run this code, we see the output of the blocking call first, then the output of the two concurrent functions. The output may be interleaved, because concurrent tasks are being run.

ECHO: "direct : 0"
ECHO: "direct : 1"
ECHO: "direct : 2"
ECHO: "goroutine : 0"
ECHO: "going"
ECHO: "goroutine : 1"
ECHO: "goroutine : 2"
ECHO: "done"

Next we’ll look at a complement to concurrent execution in programs: channels.


In OpenSCAD, we use a combination of functions and loops to simulate the behavior of concurrent execution, as OpenSCAD does not have built-in threading capabilities. The echo function is used to mimic output in the console for observing the execution flow.