Golf has an extended mode that allows using C code directly. Normally, this is not allowed, but if you use the extended-mode directive in your .golf file, you will be able to call C code from it.
In extended mode, Golf acts as a web framework for the C programming language, allowing greater flexibility in developing web applications based on this language.
Below, we present an example of how to use Golf as a web framework for C, creating a simple application that calculates the factorial of a number and exposes this functionality through a web application.
To create the application, we first generate the necessary file structure:
mkdir -p c-for-web
cd c-for-web
Next, we create a new application in Golf:
gg -k fact
Then, we save the following code in a file called calc-fact.golf. This file defines the web service that will interact with clients and call the C function:
extended-mode
%% /calc-fact public
get-param par
string-number par to num
set-number result
call-extended factorial(num, &result)
@Factorial of <<p-out par>> is <<p-num result>>
%%
Next, we create a factorial.c file with the implementation of the C function:
#include 'golf.h'
void factorial(gg_num f, gg_num *res) {
*res = 1;
gg_num i;
for (i = 2; i <= f; i++) {
*res *= i;
}
}
We also add a factorial.h file to declare the function used in the Golf code:
void factorial(gg_num f, gg_num *res);
To compile and run the server we use:
gg -q
We start the server with:
mgrg fact
We can test the application from the command line:
gg -r --req=/calc-fact/par=12 --exec --service
This returns:
Factorial of 12 is 479001600
To test it in a web browser, we configure Nginx by adding the following line in the configuration file:
location /fact/ { include /etc/nginx/fastcgi_params; fastcgi_pass unix:///var/lib/gg/fact/sock/sock; }
We restart Nginx to apply the changes:
sudo systemctl restart nginx
Accessing the URL:
https://127.0.0.1/fact/calc-fact/par=8
The result will be:
Factorial of 8 is 40320
At Q2BSTUDIO, we provide advanced technological solutions and specialized software development across various platforms and languages, facilitating the implementation of frameworks like Golf for optimizing web applications in C. Our team of experts is ready to help you develop robust and efficient solutions tailored to your business needs.





