Golf application server can be accessed via C API. Most programming languages allow for C linkage, so this makes it easy to talk to Golf server from anywhere. The
Client-API is very simple with just a few functions and a single data type. It's also MT-safe (i.e. safe for multi-threaded applications).
In this example, a Golf server will use a tree object to store key/value pairs, which can be added, queried and deleted for as long as the server is running (i.e. it's an in-memory database, or a cache server). Client will insert the key/value pairs, query and delete them.
Create a server and start it
To get started, create a directory for this example and position in it:
mkdir -p c-api
cd c-api
Copied!
Save this into a file "srv.golf":
begin-handler /srv public
silent-header
do-once
new-tree ind process-scope
end-do-once
get-param op
get-param key
get-param data
if-true op equal "add"
write-tree ind key (key) value data status st
if-true st equal GG_ERR_EXIST
@Key exists [<<p-out key>>]
else-if
@Added [<<p-out key>>]
end-if
else-if op equal "delete"
delete-tree ind key (key) value val status st
if-true st equal GG_ERR_EXIST
@Not found [<<p-out key>>]
else-if
@Deleted, old value was [<<p-out val>>]
end-if
else-if op equal "query"
read-tree ind equal (key) value val status st
if-true st equal GG_ERR_EXIST
@Not found, queried [<<p-out key>>]
else-if
@Value [<<p-out val>>]
end-if
end-if
end-handler
Copied!
Create "index" application ("-k"):