Hello World
Main Program
Traditionally the first program we write in any programming language is called a Hello World
program – a program that simply outputs the text Hello World
to the terminal.
Let's write our first program using Ballerina. Use bal run <file_name>
to run the samples.
Sample
//Binds prefix `io` to `ballerina/io` module.
import ballerina/io;
//The main function is the program entry point.
public function main() {
// Print text to the console using `io` module `println` function.
io:println("Hello World");
}
Output
Hello World
Service
The network constructs in the language make it easy to develop network interactions easily. A simple HTTP
service is as follows.
Sample
import ballerina/http;
service / on new http:Listener(9090) {
// Handles HTTP GET requests.
resource function get hello() returns string {
return "Hello World!";
}
}
Once the above service is started, invoke the service using the cURL
command below using another terminal.
curl http://localhost:9090/hello
Output
Hello World!