GOLF(2gg)
π GOLF(2gg) β the write-array page, but make it make sense π
okay bestie, so this is a page from the GOLF manual (section 2gg, development vibes β¨), and it's all about write-array. Yes, lowercase, yes with a dash, that's just how she is. Let's get into it π«Ά
πΈ NAME πΈ
write-array β (array)
π PURPOSE π
Store value into an array. That's the whole gig. She puts a thing in a place. Iconic behavior, honestly π
β¨ SYNTAX β¨
write-array <array> \
key <key> \
value <value> \
[ old-value <old value> ]
see the brackets around old-value? that means it's optional, bestie, you don't have to bring it. but you can π
π¦ DESCRIPTION π¦
so write-array stores a <value> (that's the "value" clause, keep up π) under index <key> (that's the "key" clause) into <array>. and heads up β that <array> has to already exist, which means you gotta make it first with new-array. no skipping steps, no cap π«
<key> is a number from 0 up to (but not including) the currently allocated array size β go peep new-array for that deets. and the type of <value>? that got decided the moment <array> was created (the "type" clause, iykyk), and it can be either a string, a number, or a boolean. pick your fighter β
<key> and <value> together? we call that an "element". cute lil couple, honestly π₯°
the old value sitting under index <key> gets handed back to you in <old value> (that's the "old-value" clause), and then <value> slides right in and replaces the old value. out with the old, in with the new π
okay now the important part, pay attention:
if an<array>was created with the"process-scope"clause (again, seenew-array), then that element<value>will not be freed when the current request ends. instead it persists for as long as the process runs β unless it gets deleted (seeread-arraywith thedeleteclause). so yeah, it hangs around. forever-ish. rent free π
π₯ EXAMPLES π₯
writing data to an array, the classic π«
new-array arr
write-array arr key 100 value "some data"
writing a new value with the same key index and snagging the previous value (which is "some data") π
write-array arr key 100 value "new data" old-value od
@Previous value for this key index is <<print-out od>>
the main character example: a whole array service π
okay so the following is an array service, where a process-scoped array gets created. it does inserting, deleting and querying on indexed keys. the maximum number of keys it holds is 10,000,000 (indexed from 0 to 9,999,999). and a service process like this can run indefinitely β she's in it for the long haul, periodt π
create the file arrsrv.golf:
%% /arrsrv public
do-once
new-array arr max-size 10000000 process-scope type string
end-do-once
// Get input parameters
get-param op
get-param key
get-param data
// Convert string keye to number
string-number key to key_n
if-true op equal "add" // Add data to array
write-array arr key key_n value data old-value old_data
delete-string old_data
@Added [<<print-out key>>]
else-if op equal "delete" // Delete data and obtain the value deleted
read-array arr key key_n value val delete
@Deleted [<<print-out val>>]
delete-string val
else-if op equal "query" // Query hash based on key value
read-array arr key key_n value val
@Value [<<print-out val>>]
end-if
%%
create and make the application, then run it as a service π§
// Create application
gg -k arr
// Make application
gg -q
// Start application (single process key service)
mgrg -w 1 arr
try it from a command line client (see gg) π
// Add data
gg -r --req="/arrsrv/op=add/key=15/data=15" --service --app="/arr" --exec
// Query data
gg -r --req="/arrsrv/op=query/key=15" --service --app="/arr" --exec
// Delete data
gg -r --req="/arrsrv/op=delete/key=15" --service --app="/arr" --exec
see read-array for more examples. she's got receipts π
π SEE ALSO π
- Array
new-array purge-array read-array write-array See all documentation
and that's the whole page, bestie π this one came from the GOLF manual, version $VERSION, dated $DATE β go forth and write to your arrays 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