Ballerina (programming language): Difference between revisions

Content deleted Content added
m General fixes and manual cleanup, typo(s) fixed: Github → GitHub
PubuduF (talk | contribs)
Update hello world example to Ballerina 2201.0.0 version
Line 42:
== Examples ==
 
=== Hello World Service ===
The regular Hello World program:
{{pre|
import ballerina/httpio;
 
public function main() {
service hello on new http:Listener(9090) {
check caller->respondio:println("Hello, World!");
}
}}
 
To execute the above program, place the source code in a <code>.bal</code> file and provide the path of the file to the <code>bal run</code> command.
resource function sayHello(http:Caller caller,
http:Request req) returns error? {
 
<syntaxhighlight lang="console">
check caller->respond("Hello, World!");
$ ballerina run hello_world.bal
Hello, World!
</syntaxhighlight>
 
The service version of the Hello World program:
{{pre|
import ballerina/http;
 
service hello/greet on new http:Listener(9090) {
resource function sayHelloget . (http:Caller) returns string caller,{
return "Hello World!";
}
}
}}
To start the service, navigate to the directory that contains the
`.bal` file, and execute the `ballerina run` command below.
<syntaxhighlight lang="console">
$ ballerina run hello_world.bal
[ballerina/http] started HTTP/WS listener 0.0.0.0:9090
 
Services are executed in the same manner, except they don't terminate like regular programs do. Once the service is up and running, one can use an HTTP client to invoke the service. For example, the above service can be invoked using the following cURL command:
curl http://localhost:9090/hello/sayHello
 
Hello, World!
<syntaxhighlight lang="console">
$ curl http://localhost:9090/hello/sayHellogreet
Hello World!
</syntaxhighlight>