Ballerina (programming language): Difference between revisions

Content deleted Content added
PubuduF (talk | contribs)
Add REST API example
PubuduF (talk | contribs)
Line 112:
 
=== Workers ===
<syntaxhighlight>
 
{{pre|1=
import ballerina/http;
import ballerina/lang.'int;
Line 122 ⟶ 121:
// to avoid deadlocks.
public function main() {
@strand {thread: "any"}
worker w1 {
int w1val = checkpanic calculate("2*3");
Line 141:
// A worker can have an explicit return type, or else, if a return type is not mentioned,
// it is equivalent to returning ().
@strand {thread: "any"}
worker w2 {
int w2val = checkpanic calculate("17*5");
Line 161 ⟶ 162:
 
function calculate(string expr) returns int|error {
http:Client httpClient = check new ("https://api.mathjs.org");
varstring response = check httpClient->get(string `/v4/?expr=${expr}`);
return check 'int:fromString(check <@untainted> response.getTextPayload());
}
</syntaxhighlight>
}}<ref name="example2">{{cite web |url=https://github.com/ballerina-platform/ballerina-distribution/blob/master/examples/worker-interaction/worker_interaction.bal |title= Worker interaction |author=Ballerina Team|date=16 September 2020 |publisher=ballerina.io}}</ref>
 
=== gRPC Unary Blocking ===