Here’s the translation of the Go code to Ada, formatted in Markdown suitable for Hugo:
Our first example demonstrates how to spawn external processes in Ada. Here’s the full source code:
This Ada program demonstrates how to spawn external processes and capture their output. Let’s break down the key parts:
We define a function Execute_Command that takes a command string as input, executes it using GNAT.OS_Lib.Spawn, and returns the output as a string.
We start with a simple command that takes no arguments: date. We execute it and print its output.
Next, we demonstrate error handling by trying to execute date -x, which is an invalid command. We check if the execution failed and print an appropriate message.
We then show how to pipe data to a command. In this case, we use echo to pipe input to grep.
Finally, we execute a more complex command: ls -a -l -h.
To run this program, save it as spawn_processes.adb and compile it using your Ada compiler. For example, with GNAT:
The output will be similar to running these commands directly from the command line:
This example demonstrates how to spawn external processes in Ada, capture their output, and handle potential errors. It’s important to note that Ada’s standard library doesn’t provide as direct support for process spawning as Go does, so we use the GNAT-specific GNAT.OS_Lib package for this functionality.