Sunday, September 29, 2024

SQLite with Golf


This example will create a service that inserts key and value into SQLite table. It's tested from command line.

Create a directory and then switch to it:
mkdir sqlite
cd sqlite

Setup SQLite database in file "mydata.db":
echo 'drop table if exists key_value; 
create table if not exists key_value (key varchar(50) primary key, value varchar(100));' | sqlite3 mydata.db

Create configuration file "mydb" that describes the SQLite database "mydata.db":
echo "$(pwd)/mydata.db"> mydb

Create the application:
sudo mgrg -i -u $(whoami) sqlite

Create file insert.golf:
vim insert.golf

Copy and paste to it (note use of database configuration "mydb" in @mydb):
 %% /insert public
     get-param key
     get-param value
     run-query @mydb = "insert into key_value(key,value) values ('%s', '%s')" input key, value error err affected-rows aff_rows no-loop
     @Error <<p-out err>>, affected rows <<p-num aff_rows>>
 %%

Compile the application - we specify that file "mydb" is describing SQLite database:
gg -q --db=sqlite:mydb

Run the application by executing this service from command line. Note passing the input parameters "key" and "value" to it:
gg -r --req="/insert/key=1/value=one" --exec --silent-header

The output is:
Error 0, affected rows 1

Verify data inserted:
echo -e ".headers off\n.mode line\nselect key "Key", value "Value" from key_value"|sqlite3 mydata.db

The result:
Key = 1
Value = one

Copy-code icon added

Copy icon () is now in the upper right corner of all code blocks in Golf documentation, as well as this blog.

Click this icon to copy the code. This is useful for getting the sample code in your own, learning Golf etc.

Friday, September 27, 2024

Golf 44 released

Bug fixes:
  • Fixed bug where a memory for parameters or split-string type could be allocated with no data causing invalid write in debug mode only. 
  • Fixed bug where passing a number from sub-request back to the caller would not be correct or could cause invalid memory read.
  • Fixed bug in encryption statement (encrypt-data) with checking memory bounds of Initialization Vector (IV).
  • Fixed bug in executing SQL with any database. The bug was with checking the formatting of the statement.
New features and improvements:
  • Added -z command-line option to Service Manager (mgrg). This option suppresses HTTP header  for all service requests in application (equivalent to implicit silent-header statement at the beginning of each request handler).
  • Improved performance of piping output from execution of programs in exec-program statement.
  • Improved performance of message composition in write-message statement.
  • Structured types can now be returned to caller from sub-handler - for instance creating a new index or array.

Thursday, September 26, 2024

Overview of Golf


Golf is a new programming language and framework for developing web services and web applications. The reason for Golf is to make software development easier, more reliable and to improve run-time performance. To do this, Golf is a very high level language yet it's a high performance one; those two qualities aren't usually together.

Golf is a declarative language designed for simplicity. That means top-down approach, rather than bottom-up: it's more about describing what to do than coding it. It's a modeling language where pieces are assembled together quickly and with confidence. It's about the framework to create and deploy web services with less effort and faster.

Golf is a memory-safe language. Your program is safe from overwriting memory it shouldn't overwrite, and it won't leave dangling pointers hanging around. Golf is a static-typed language with only three basic types (strings, numbers and boolean) and (currently) the following structured types: service, message, array, index, index-cursor, fifo, lifo, list, split-string and file.

Golf is also a high-performance compiled language, designed to create fast and small native executables without interpreters or p-code.

Memory safe languages often suffer performance bottlenecks due to the fact that range checking, garbage collection and other memory management techniques do take their toll.

Golf is designed from ground up to alleviate these issues. Firstly, the best way not to lose performance on expensive memory management is not to have one. By default, Golf has a light-weight memory safety implementation, and you can expand it if your system is short on memory. In addition, its run-time libraries are written entirely in C and the run-time overhead comes at the input and output of Golf statements, and not within libraries. Since libraries do most of the run-time work, the impact of imposing memory safety is minimal.

Underlying Golf's functionality are industry-standard Open Source libraries, such as SSL, Curl, MariaDB and others, in addition to native Golf's.

In extended mode, Golf is extensible with any standard libraries (with C interop), which means most programming languages (including C/C++, Rust etc.). In this mode, Golf (obviously) does not guarantee memory safety, but it does not necessarily mean it's not safe either.

Golf is very simple to work with - it doesn't even have expressions in a sense other languages do, save for very basic integer expressions (with plus, minus, divide, multiply). This is by design to reduce complexity and improve performance. Golf's statements aim to deliver complete functionality without complicated coding, and are customizable to a great extent, statically optimized at compile-time for performance.

Golf home page is at https://golf-lang.github.io and is Free Open Source Software licensed under Apache 2 license.

Tuesday, September 24, 2024

Golf 37 released

  • Added "new-line" clause to output statements, such as p-out, p-web, p-url, p-path and p-num. This clause adds new line to the output of any of these statements.
  • Fixed bug in write-string where program errors out in some cases with multiple use of encoded output statements, such as p-web for instance.

Saturday, September 21, 2024

Web service Hello World


To access a Golf service on the web, you need to have a web server or load balancer (think Apache, Nginx, HAProxy etc.).

This assumes you have completed the Hello World as a Service, with a service built and tested via command line.

In this example, Nginx web server is used; edit its configuration file. For Ubuntu and similar:
sudo vi /etc/nginx/sites-enabled/default

while on Fedora and other systems it might be:
sudo vi /etc/nginx/nginx.conf

Add the following in the "server {}" section:
location /hello/ { include /etc/nginx/fastcgi_params; fastcgi_pass  unix:///var/lib/gg/hello/sock/sock; }

"hello" refers to your Hello World application. Finally, restart Nginx:
sudo systemctl restart nginx

Now you can call your web service, from the web. In this case it's probably a local server (127.0.0.1) if you're doing this on your own computer. The URL would be:
http://127.0.0.1/hello/hello-world/name=Mike

Note the URL request structure: first comes the application path ("/hello") followed by request path ("/hello-world") followed by URL parameters ("/name=Mike"). The result:

Hello World as a Service


Writing a service is the same as writing a command-line program in Golf. Both take the same input and produce the same output, so you can test with either one to begin with.

For that reason, create first Hello World as a command-line program.

The only thing to do afterwards is to start up Hello World as application server:
mgrg hello

Now there's a number of resident processes running, expecting clients requests. You can see those processes:
ps -ef|grep hello

The result:
bear       25772    2311  0 13:04 ?        00:00:00 mgrg hello
bear       25773   25772  0 13:04 ?        00:00:00 /var/lib/gg/bld/hello/hello.srvc
bear       25774   25772  0 13:04 ?        00:00:00 /var/lib/gg/bld/hello/hello.srvc
bear       25775   25772  0 13:04 ?        00:00:00 /var/lib/gg/bld/hello/hello.srvc
bear       25776   25772  0 13:04 ?        00:00:00 /var/lib/gg/bld/hello/hello.srvc
bear       25777   25772  0 13:04 ?        00:00:00 /var/lib/gg/bld/hello/hello.srvc

"mgrg hello" runs the Golf process manager for application "hello". A number of ".../hello.srvc" processes are server processes that will handle service request sent to application "hello".

Now, to test your service, you can send a request to the server from command line (by using "--service" option):
gg -r --req="/hello-world/name=Mike" --exec --silent-header --service

The above will make a request to one of the processes above, which will then reply:
This is Hello World from Mike

Friday, September 20, 2024

Golf 32 released

  • HTTP header is now sent to client by default at the very first output, i.e. the equivalent of 'out-header default' statement.No code change is necessary. Typical use of out-header is to send default headers at the very beginning of a service, and those can in general be removed now, since it's done automatically.

  • Color scheme is now supported for Golf source code. Prior to this release, there was one hardcoded colorscheme. Now your can use any available color  scheme to customize look and feel of source code in Vim editor (use ":colorscheme" command).

Thursday, September 19, 2024

Using Vim color schemes with Golf


Golf installation comes with a Vim module for highlighting Golf code. After installing Golf, run this to install the Vim module:
gg -m

The default color scheme looks like this:
To change color scheme, type ":colorscheme " in command mode, then press Tab to see available color schemes. Press Enter to choose one. For instance, in 'darkblue' color scheme, it may look like:
To make the change permanent, edit file ".vimrc" in home directory:
vi ~/.vimrc

and append line:
colorscheme darkblue

Tuesday, September 17, 2024

Hello World in Golf


Create a directory for your Hello World application and then switch to it:
mkdir hello-world
cd hello-world

Create the application:
sudo mgrg -i -u $(whoami) hello

Create a file hello-world.golf:
vim hello-world.golf

and copy this code to it:
 begin-handler /hello-world public
     get-param name
     @This is Hello World from <<p-out name>>
 end-handler

This service takes input parameter "name" (see get-param), and then outputs it along with a greeting message (see output-statement).

Compile the application:
gg -q

Run the application by executing this service from command line. Note passing the input parameter "name" with value "Mike":
gg -r --req="/hello-world/name=Mike" --exec --silent-header

The output is:
This is Hello World from Mike

Golf is at https://golf-lang.github.io/.

Friday, September 13, 2024

Introduction to Golf


Golf is a new programming language and framework for developing web services and web applications. It is:
  • declarative language designed for simplicity,
  • memory-safe,
  • functional statically-typed,
  • high-performance compiled language, designed to create fast and small native executables without interpreters or p-code,
  • built on top of industry-standard Open Source libraries, such as SSL, Curl, MariaDB and others, in addition to native Golf's,
  • extensible with any standard libraries,
  • very simple to work with by design to reduce comlexity and improve performance.

Thursday, September 12, 2024

Initial Golf release

Version 25 is an initial Golf release.

The official web site is at golf-lang.github.io. It is also accessible (for now, but not indefinitely) from vely.dev since Golf evolved from Vely. Golf and Vely are copyright (c) Gliim LLC licensed under their respective licenses and developed by the same team.

Git source code repository is at github.com/golf-lang/golf.

Golf is licensed under Apache 2 Free Open Source license.