Basic Genie Samples
Hello World
A very simple Hello World program:
init
print ("hello, world\n")
Compile and Run
$ valac hello.gs $ ./hello
If the binary should have a different name:
$ valac hello.gs -o greeting $ ./greeting
Reading User Input
init
stdout.printf ("Please enter your name: ")
name : string = stdin.read_line ()
if name is not null
stdout.printf ("Hello, %s!\n", name)
Genie provides the objects stdin (standard input), stdout (standard output) and stderr (standard error) for the three standard streams. The printf method takes a format string and a variable number of arguments as parameters.
Mathematics
Math functions are inside the Math namespace.
init
stdout.printf ("Please enter the radius of a circle: ")
radius : double = double.parse (stdin.read_line ())
stdout.printf ("Circumference: %g\n", 2 * Math.PI * radius)
stdout.printf ("sin(pi/2) = %g\n", Math.sin (Math.PI / 2))
// Random numbers
stdout.printf ("Today's lottery results:")
for var i = 0 to 6
stdout.printf (" %d", Random.int_range (1, 49))
stdout.printf ("\n")
stdout.printf ("Random number between 0 and 1: %g\n", Random.next_double ())
Command-Line Arguments and Exit Code
init
// Output the number of arguments
stdout.printf ("%d command line argument(s):\n", args.length)
// Enumerate all command line arguments
for s in args
stdout.printf ("%s\n", s)
The first command line argument (args[0]) is always the invocation of the program itself.
Reading and Writing Text File Content
This is very basic text file handling. For advanced I/O you should use GIO's powerful stream classes.
init
try
filename : string = "data.txt"
// Writing
content : string = "hello, world"
FileUtils.set_contents (filename, content)
// Reading
read : string
FileUtils.get_contents (filename, out read)
stdout.printf ("The content of file '%s' is:\n%s\n", filename, read)
except e : FileError
stderr.printf ("%s\n", e.message);
Spawning Processes
init
try
// Non-blocking
Process.spawn_command_line_async ("ls")
// Blocking (waits for the process to finish)
Process.spawn_command_line_sync ("ls")
// Blocking with output
standard_output : string
standard_error : string
exit_status : int
Process.spawn_command_line_sync (
"ls", out standard_output, out standard_error, out exit_status)
except e : SpawnError
stderr.printf ("%s\n", e.message)
First Class
/* class derived from GObject */
class BasicSample : Object
/* public instance method */
def run ()
stdout.printf ("Hello World\n")
/* application entry point */
init
// instantiate this class, assigning the instance to
// a type-inferred variable
var sample = new BasicSample ()
// call the run method
sample.run ()
// return from this main method
The entry point may as well be inside the class, if you prefer it this way:
class BasicSample : Object
def run ()
stdout.printf ("Hello World\n")
def static main ()
var sample = new BasicSample ();
sample.run ();
In this case main must be declared static.