Functions and parameters in Golf
In Golf, there are no "functions" or "methods" as you may be used to in other languages. Any encapsulation is a request handler - you can think of it as a simple function that executes to handle a request - it can an external request (i.e. from an outside caller such as web browser, web API, or from a command-line), or internal requests from another handler in your application.
By the same token, there are no formal parameters in a way that you may be used to. Instead, there are named parameters, basically name/value pairs, which you can set or get anywhere during the request execution. In addition, your request handler can handle the request body, environment variables, the specific request method etc. (see request). Here though, we'll focus on parameters only.
You'll use set-param to set a parameter, which can then be obtained anywhere in the current request, including in the current handler's caller or callee. Use get-param to obtain a parameter that's set with set-param.
Parameters are very fast - they are static creatures implemented at compile time, meaning only fixed memory locations are used to store them (making for great CPU caching), and any name-based resolution is used only when necessary, and always with fast hash tables and static caching.
Calling one handler from another
In this article, we'll talk about call-handler which is used to call a handler from within another handler.
Here you'll see a few examples of passing input and output parameters between requests handlers. These handlers are both running in the same process of an application (note that application can run as many processes working in parallel). To begin, create an application:
mkdir param
cd param
gg -k param
Copied!
You'll also create two source files ("local.golf" and "some.golf") a bit later with the code below.
Let's start with a simple service that provides current time based on a timezone as an input parameter (in file "local.golf"):
begin-handler /local/time
get-param tzone default-value "MST"
get-time to curr_time timezone tzone
@<div>Current time is <<print-out curr_time>></div>
end-handler
Copied!
In this case, HTML code is output. Make the application:
gg -q --public
Copied!
and run it from command line (just as if it were called from a web browser):
gg -r --req="/local/time" --silent-header --exec
Copied!
with the result something like:
<div>Current time is Mon, 02 Dec 2024 16:27:03 EST</div>
Copied!
Calling request from another, example #1
In this example, one request handler calls another from within the same process of an application using call-handler.
For instance, here's the caller code which calls the above "/local/time" service (in file "some.golf"):
begin-handler /some/service
set-param tzone="EST"
call-handler "/local/time"
end-handler
Copied!
and in this case the output of the "/local/time" would simply become output of "/some/service" (and be presumably sent to a client like web browser).
You can also, however, return the string to the caller using set-param - here's the reworked "/local/time" service (in file "local.golf"):
begin-handler /local/time
get-param tzone default-value "MST"
get-time to curr_time timezone tzone
set-param curr_time
end-handler
Copied!
The result parameter will be obtained in the caller, and then output as HTML from there (we could save it to a file instead, or whatever), so here's what it'd be now in file "some.golf":
begin-handler /some/service
set-param tzone="EST"
call-handler "/local/time"
get-param curr_time
@<div>Current time is <<print-out curr_time>></div>
end-handler
Copied!
Make the project and run it:
gg -q --public
gg -r --req="/some/service" --silent-header --exec
Copied!
with the similar end result.
Calling request from another, example #2
Consider this handler, which checks if number is even, and returns true or false (file "check.golf"):
begin-handler /check/even
get-param num type number
if-true num every 2
set-param is_even = true
else-if
set-param is_even = false
end-if
end-handler
Copied!
Here's calling it from another handler (add this to file "some.golf"):
begin-handler /some/task
set-param num = 23
call-handler "/check/even"
get-param is_even type bool
if-true is_even equal true
@EVEN
else-if
@ODD
end-if
end-handler
Copied!
Make the project and run it:
gg -q --public
gg -r --req="/some/task" --silent-header --exec
Copied!
with the result (since 23 is an odd number):
ODD
Copied!
Golf is a language and a platform built to provide native web services, and everything in it is centered around the notion of handling service calls. Simple name/value constructs rule the communication between handlers on the network as well as locally. Suffice it to say, Golf's not a classic C-like programming language (though ironically is built in C and compiles to C).
Articles
article-capi
article-cookies
article-debug
article-distributed
article-encryption
article-fetch-web-page
article-fifo
article-file-manager
article-hello-server
article-hello-world
article-hello-world-service
article-hello-world-service-web
article-how-to-create-golf-application
article-json
article-language
article-mariadb
article-memory-safety
article-memory-safety-web
article-notes-postgres
article-random
article-regex
article-remote-call
article-request-function
article-security
article-sendmail
article-server
article-shopping
article-sqlite
article-statements
article-status-check
article-tree
article-tree-web
article-vim-coloring
article-web-framework-for-c-programming-language
article-what-is-golf
article-what-is-web-service
See all
documentation
Copyright (c) 2019-2025 Gliim LLC. All contents on this web site is "AS IS" without warranties or guarantees of any kind.