GOLF(2gg)
β¨ NAME β¨
Server-API β Golf documentation (API). That's the whole vibe, bestie. This is the manual page GOLF(2gg) from the Development section, and it's literally all about how to link your non-Golf code into your Golf app. π
π DESCRIPTION π
okay so first thing: Golf can be used in extended-mode, which is the mode where non-Golf code or libraries get linked right into your application. π¦
That code can come from a library (peep the --llflag and --cflag options in gg), or it can just be straight-up C code you wrote yourself β like files with .c and .h extension hanging out next to your Golf application. To pull that off, you use the call-extended statement. π
Any function with C linkage is fair game, as long as:
- its parameters (whether by value or by reference) are only of type
int64_t(that's the number type in Golf),bool(the bool type in Golf) orchar *(the string type in Golf). - it must not return any value at all β like, it has to have a
voidreturn type. no exceptions. π
When you're allocating strings in extended code, you must use Golf memory management functions. These are based on the standard C library (things like malloc or free), but they are not compatible with them, because Golf manages that memory on top of the standard C library. so don't go mixing them, fr fr. π
The functions you can use are:
char *gg_strdup (char *s)β creates a copy of a null-terminated strings. A pointer to memory data is returned. β¨char *gg_strdupl (char *s, gg_num from, gg_num l)β creates a copy of memory data pointed to bys, starting from bytefromof lengthl. Note thatfromis indexed from 0. A pointer to memory data is returned. π«void *gg_malloc(size_t size)β allocates memory of sizesizeand returns a pointer to it.void *gg_calloc(size_t nmemb, size_t size)β allocatesnmembblocks of memory (each of sizesize) and returns a pointer to it. Memory is initialized to all zero bytes. squeaky clean. π§Όnum gg_mem_get_id (void *ptr)β returns the Golf memory handle for memoryptr.void *gg_realloc(gg_num r, size_t size)β reallocates memory identified with Golf memory handler(seegg_mem_get_id()) to a new size ofsizeand returns a pointer to it. Note that you can only reallocate the memory you created withgg_malloc()andgg_calloc()β do not attempt to reallocate Golf memory passed to your function. Golf in general never reallocates any existing memory in any statement. periodt. π«void gg_mem_set_len (gg_num r, gg_num len)β sets the length of memory identified with Golf memory handlertolenbytes. Note that all Golf memory must have a null-byte at the end for consistency, regardless of whether such memory holds pure binary data or an actual null-delimited string. So for example, a string"abc"would havelenset to 4 to include a null byte, and binary data"\xFF\x00\x01"(which consists of 3 bytes, the middle of which is a null byte) would havelenalso set to 4 and you would place an extra zero byte at the end of it even if it's not part of the actual useful data. Note that whatever memory length you set, it must be lesser or equal to the length of memory you have actually allocated. πnum gg_mem_get_len (gg_num r)β returns the length of memory identified with Golf memory handler. The length returned is 1 bytes less than the memory set bygg_mem_set_len(), so for example for string"abc"the return value would be 3, as it would be for"\xFF\x00\x01".
You can use gg_malloc(), gg_calloc() and gg_realloc() to create new Golf-compatible memory β and assuming you have set the last byte of any such memory to a null byte, the resulting memory will be properly sized for Golf usage. π«Ά
If you have memory that's already provided from elsewhere, you can use gg_strdup() or gg_strdupl() to create a copy of it that's compatible with Golf. π
If Golf memory you created with these functions has extra unused bytes, you can use either gg_realloc() to reduce its footprint, or you can use gg_mem_set_len() to set its length. the way you have options?? obsessed. πΈ
Note: if you use C code included with a Golf project, you must include"golf.h"file in each of them. You do not need to manually include any other".h"files (header files), as they will be automatically picked up. iconic behaviour, honestly. π
π EXAMPLES π
Place the following files in a separate directory for demonstration purposes.
In this example, "example.golf" will use C functions from "example.c", and "example.h" will have declarations of those functions. File "example.c" implements a factorial function, as well as a function that will store the factorial result in an output message that's allocated and passed back to your Golf code:
#include "golf.h"
void get_factorial(gg_num f, gg_num *res)
{
*res = 1;
gg_num i;
for (i = 2; i <= f; i++) {
*res *= i;
}
}
#define MEMSIZE 200
void fact_msg (gg_num i, char **res)
{
// golf rule: outgoing string must NOT free or realloc its incoming value
// all else is allowed
char *r = gg_malloc (MEMSIZE);
gg_num f;
get_factorial (i, &f);
gg_num bw = snprintf(r, MEMSIZE, "Factorial value (message from C function) is %ld", f) + 1;
// reduce memory footprint to match the memory used (including the null byte)
// you can also use gg_mem_set_len() with bw as length for higher performance
// (but also higher memory usage)
*res = gg_realloc (gg_mem_get_id(r), bw);
}
File "example.h" declares the above functions:
void get_factorial(gg_num f, gg_num *res);
void fact_msg (gg_num i, char **res);
File "example.golf" will call the above functions and display the results:
extended-mode
begin-handler /example public
set-number fact
call-extended get_factorial (10, &fact)
@Factorial is <<print-out fact>>
set-string res
call-extended fact_msg (10, &res)
print-out res
@
end-handler
Create application "example":
sudo mgrg -i -u $(whoami) example
Make the application:
gg -q
Run it:
gg -r --req="/example" --exec --silent-header
The output is, as expected:
Factorial is 3628800
Factorial value (message from C function) is 362880
π¦ SEE ALSO π¦
APIClient-APIServer-API- See all documentation
and that's the whole page, bestie π
this cutie was served up on $DATE at version $VERSION of the GOLF(2gg) manual β go forth and call-extended responsibly. mwah. πβ¨π
π¦ where this page comes from
- ubuntu questing: golf 601.4.41-1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms