<commit_before>== Basics ==

=== Hello world ===

Golo source code need to be placed in _modules_. Module names are
separated with dots, as in:

[source,text]
-------------
Foo
foo.Bar
foo.bar.Baz
(...)
-------------

It is suggested yet not enforced that the first elements in a module
name are in lowercase, and that the last one have an uppercase first
letter.

A Golo module can be executable if it has a _function_ named `main` and
that takes an argument for the JVM program arguments:

[source,text]
------------------------- 
module hello.World

function main = |args| {
  println("Hello world!")
}
-------------------------

`println` is a predefined function that outputs a value to the standard
console. As you can easily guess, here we output `Hello, world!` and
that is an awesome achievement.

=== Running _"Hello world"_ with `gologolo` ===

Of course, we need to run this incredibly complex application.

Golo comes with a `gologolo` script found in the distribution `bin/`
folder. Provided that it is available from your current `$PATH`, you may
simply invoke it as follows:

[source,console]
----------------------------------
$ gologolo samples/helloworld.golo
Hello world!
$
----------------------------------

`gologolo` takes several Golo source files as input. It expects the last
one to have a `main` function to call. The Golo code is compiled on the
fly and executed straight into a JVM.

You may also pass arguments to the `main` function by appending `--args`
on the command line invocation. Suppose that we have a module `EchoArgs`
as follows:

[source,text]
------------------------------------------------- 
module EchoArgs

function main = |args| {
  for (var i = 0, i < alength(args), i = i + 1) {
    println("#" + i + " -> " + aget(args, i))
  }
}
-------------------------------------------------

We may invoke it as follows:

[source,console]
-----------------------------------------------------
$ gologolo samples/echo-args.golo --args plop da plop
#0 -> plop
#1 -> da
#2 -> plop
$
-----------------------------------------------------

Note that `args` is expected to be an array, and we can use built-in
functions to deal with it: `alength` gives the length of an array while
`aget` gives the value of an element at a given index.

=== Compiling Golo source code ===

Golo comes with a `goloc` compiler that generates JVM bytecode in
`.class` files. We will give more details in the chapter on
interoperability with Java.

Compiling Golo files is straightforward:

[source,console]
-----------------------------------------------
$ goloc -output classes samples/helloworld.golo
$
-----------------------------------------------

This compiles the code found in `samples/helloworld.golo` and outputs
the generated classes to a `classes` folder (it will be created if
needed):

[source,console]
-------------------
$ tree classes/
classes/
└── hello
    └── World.class

1 directory, 1 file
$
-------------------

=== Running compiled Golo code ===

Golo provides a `golo` command for running compiled Golo code:

[source,console]
------------------
$ cd classes
$ golo hello.World
Hello world!
$
------------------

Simple, isn't it?

=== Variables and constants ===

Golo does not check for types at compile time, and they are not
declared. Everything happens at runtime in Golo.

Variables are declared using the `var` keyword, while constants are
declared with `let`. It is strongly advised that you favour `let` over
`var` unless you are certain that you need mutability.

Variables and constants need to be initialized when declared. Failing to
do so results in a compilation error.

Here are a few examples:

[source,text]
-------------
# Ok
var i = 3
i = i + 1

# The assignment fails because truth is a constant
let truth = 42
truth = 666

# Invalid statement, variables / constants have to be initialized
var foo
-------------

=== Data literals ===

Golo supports a set of data literals. They directly map to their counterparts from the Java Standard
API. We give them along with examples in <<data-literals,the data literals table>> below.

[options="header",id="data-literals"]<commit_msg>Clarification on valid / wrong variable ids.<commit_after>== Basics ==

=== Hello world ===

Golo source code need to be placed in _modules_. Module names are
separated with dots, as in:

[source,text]
-------------
Foo
foo.Bar
foo.bar.Baz
(...)
-------------

It is suggested yet not enforced that the first elements in a module
name are in lowercase, and that the last one have an uppercase first
letter.

A Golo module can be executable if it has a _function_ named `main` and
that takes an argument for the JVM program arguments:

[source,text]
------------------------- 
module hello.World

function main = |args| {
  println("Hello world!")
}
-------------------------

`println` is a predefined function that outputs a value to the standard
console. As you can easily guess, here we output `Hello, world!` and
that is an awesome achievement.

=== Running _"Hello world"_ with `gologolo` ===

Of course, we need to run this incredibly complex application.

Golo comes with a `gologolo` script found in the distribution `bin/`
folder. Provided that it is available from your current `$PATH`, you may
simply invoke it as follows:

[source,console]
----------------------------------
$ gologolo samples/helloworld.golo
Hello world!
$
----------------------------------

`gologolo` takes several Golo source files as input. It expects the last
one to have a `main` function to call. The Golo code is compiled on the
fly and executed straight into a JVM.

You may also pass arguments to the `main` function by appending `--args`
on the command line invocation. Suppose that we have a module `EchoArgs`
as follows:

[source,text]
------------------------------------------------- 
module EchoArgs

function main = |args| {
  for (var i = 0, i < alength(args), i = i + 1) {
    println("#" + i + " -> " + aget(args, i))
  }
}
-------------------------------------------------

We may invoke it as follows:

[source,console]
-----------------------------------------------------
$ gologolo samples/echo-args.golo --args plop da plop
#0 -> plop
#1 -> da
#2 -> plop
$
-----------------------------------------------------

Note that `args` is expected to be an array, and we can use built-in
functions to deal with it: `alength` gives the length of an array while
`aget` gives the value of an element at a given index.

=== Compiling Golo source code ===

Golo comes with a `goloc` compiler that generates JVM bytecode in
`.class` files. We will give more details in the chapter on
interoperability with Java.

Compiling Golo files is straightforward:

[source,console]
-----------------------------------------------
$ goloc -output classes samples/helloworld.golo
$
-----------------------------------------------

This compiles the code found in `samples/helloworld.golo` and outputs
the generated classes to a `classes` folder (it will be created if
needed):

[source,console]
-------------------
$ tree classes/
classes/
└── hello
    └── World.class

1 directory, 1 file
$
-------------------

=== Running compiled Golo code ===

Golo provides a `golo` command for running compiled Golo code:

[source,console]
------------------
$ cd classes
$ golo hello.World
Hello world!
$
------------------

Simple, isn't it?

=== Variables and constants ===

Golo does not check for types at compile time, and they are not
declared. Everything happens at runtime in Golo.

Variables are declared using the `var` keyword, while constants are
declared with `let`. It is strongly advised that you favour `let` over
`var` unless you are certain that you need mutability.

Variables and constants need to be initialized when declared. Failing to
do so results in a compilation error.

Here are a few examples:

[source,text]
-------------
# Ok
var i = 3
i = i + 1

# The assignment fails because truth is a constant
let truth = 42
truth = 666

# Invalid statement, variables / constants have to be initialized
var foo
-------------

Valid names contain upper and lower case letters within the `[a..z]` range, underscores (`_`),
dollar symbols (`$`) and numbers. In any case, an identifier must not start with a number.

[source,text]
---------------------------------------------
# Ok, but not necessarily great for humans...
let _$_f_o_$$666 = 666

# Wrong!
let 666_club = 666
---------------------------------------------

=== Data literals ===

Golo supports a set of data literals. They directly map to their counterparts from the Java Standard
API. We give them along with examples in <<data-literals,the data literals table>> below.

[options="header",id="data-literals"]
{'fraction_non_alphanumeric': 0.20689259046524985, 'fraction_numerical': 0.004595060310166571, 'mean_word_length': 2.243666169895678, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2534026', 'n_tokens_mistral': 2328, 'n_tokens_neox': 2255, 'n_words': 1115}