Ballerina: Hello World

Preface

Ballerina is a fully open source compile time strong type language with the vision of making it easy for programmers in the cloud native era to write the software they want
Open source address: https://github.com/ballerina-platform/ballerina-lang

Example

Download the package of corresponding platform for installation

https://ballerina.io/downloads/

This is installed in ubuntu. After downloading the deb package, install it

lan@lan-machine:~$ sudo dpkg -i ballerina-linux-installer-x64-1.1.0.deb 
[sudo] password for lan: 
Selecting previously unselected package ballerina-1.1.0.
(Reading database ... 187196 files and directories currently installed.)
Preparing to unpack ballerina-linux-installer-x64-1.1.0.deb ...
Unpacking ballerina-1.1.0 (1.1.0) ...
Setting up ballerina-1.1.0 (1.1.0) ...
lan@lan-machine:~$ ballerina version
jBallerina 1.1.0
Language specification 2019R3
Ballerina tool 0.8.0

You can see that the version of ballerina installed is 1.1.0

Hello World Main

  1. This step is not necessary to create and enter the ballerina directory, but it is recommended to do so in order to organize the files
lan@lan-machine:/disk/note$ mkdir ballerina && cd ballerina
lan@lan-machine:/disk/note/ballerina$
  1. Create the Hello world.bal file and write the corresponding code of Hello World Print
    lan@lan-machine:/disk/note/ballerina$ touch hello-world.bal
import ballerina/io;
public function main() {
    io:println("Hello, World!");
}
  1. Running Hello World ``
lan@lan-machine:/disk/note/ballerina$ ballerina run hello-world.bal 
Compiling source
        hello-world.bal

Generating executables
Running executables

Hello, World!  

As you can see, Hello World has been successfully entered, and the simplest example is finished.

Hello World Service

The second example of Hello world is to start an http server listening to port 9090

  1. Create the hello-world-service.bal file and write the corresponding code

lan@lan-machine:/disk/note/ballerina$ touch hello-world-service.bal

import ballerina/http;
import ballerina/log;
service hello on new http:Listener(9090) {

    resource function sayHello(http:Caller caller, http:Request req) {

        var result = caller->respond("Hello, World!");

        if (result is error) {
            log:printError("Error sending response", result);
        }
    }
}
  1. Run hello-world-service.bal
lan@lan-machine:/disk/note/ballerina$ ballerina run hello-world-service.bal
Compiling source
        hello-world-service.bal

Generating executables
Running executables

[ballerina/http] started HTTP/WS listener 0.0.0.0:9090

lan@lan-machine:~$ curl http://localhost:9090/hello/sayHello
Hello, World!

The composition of the path is as follows,

  1. http:Listener(9090) forms listening port 9090, that is, localhost:9090
  2. service hello forms the first path
  3. resource function sayHello forms the second path

After the whole composition http://localhost:9090/hello/sayHello

Hello World Paraller

The third example of Hello world is the execution of asynchronous tasks

  1. Create the hello-world-parallel.bal file and write the corresponding code
    lan@lan-machine:/disk/note/ballerina$ touch hello-world-paraller.bal
import ballerina/io;
public function main() {
    worker w1 {
        io:println("Hello, World! #m");
    }

    worker w2 {
        io:println("Hello, World! #n");
    }
    worker w3 {
        io:println("Hello, World! #k");
    }
}
  1. Run the hello-world-parallel.bal file
lan@lan-machine:/disk/note/ballerina$ ballerina run hello-world-paraller.bal
Compiling source
        hello-world-paraller.bal

Generating executables
Running executables

Hello, World! #m
Hello, World! #n
Hello, World! #k
lan@lan-machine:/disk/note/ballerina$ 
lan@lan-machine:/disk/note/ballerina$ 
lan@lan-machine:/disk/note/ballerina$ ballerina run hello-world-paraller.bal
Compiling source
        hello-world-paraller.bal

Generating executables
Running executables

Hello, World! #n
Hello, World! #m
Hello, World! #k

You can see that the order of each printing is not the same

Hello World Client

The last example of Hello world is an http client request

  1. Create the hello-world-client.bar file and write the corresponding code lan@lan-machine:/disk/note/ballerina$ touch hello-world-client.bal
import ballerina/http;
import ballerina/io;
public function main() {
    http:Client clientEP = new ("http://www.mocky.io");

    var resp = clientEP->get("/v2/5ae082123200006b00510c3d/");

    if (resp is http:Response) {
        var payload = resp.getTextPayload();
        if (payload is string) {

            io:println(payload);
        } else {

            io:println(payload.detail());
        }
    } else {

        io:println(resp.detail());
    }
}
  1. Run the hello-world-client.bal file
lan@lan-machine:/disk/note/ballerina$ ballerina run hello-world-client.bal
Compiling source
        hello-world-client.bal

Generating executables
Running executables

Hello World

In the example, http://www.mocky.io/v2/5ae082123200006b00510c3d/ is requested and the result is printed out

More example s can be found in Official website find

Originating from Four coffee beans Release! Pay attention to the public number - > four coffee beans] get the latest content.

Tags: sudo Linux github Ubuntu

Posted on Wed, 12 Feb 2020 03:50:00 -0500 by TwistedLogix