GOLF(2gg)
π GOLF(2gg) β write-hash π
okay bestie, welcome to the 2gg manual, this one's the write-hash page and it's literally the moment you've been waiting for π
β¨ NAME β¨
write-hash β (hash)
πΈ PURPOSE πΈ
Store a key/value pair into a hash. that's it, that's the whole vibe π
π SYNTAX π
write-hash <hash> \
key <key> \
value <value> \
[ status <status> ] \
[ old-value <old value> ]
π¦ DESCRIPTION π¦
write-hash will store the string <key> (in the key clause) and <value> (in the value clause) into <hash>, and likeβ¦ that hash must have been created with new-hash first, no cap. π₯
<key> and <value> are collectively called an "element". iconic duo, honestly.
If <key> already exists in the hash, then the old value associated with it gets returned in the string <old value> (in the old-value clause) and <value> will replace the old value β and in that case the <status> number (in the status clause) has a value of GG_INFO_EXIST. π she said out with the old.
If <key> did not exist, <status> will be GG_OKAY and <old value> is unchanged. nothing moved, nothing changed, we love a chill outcome π
If a <hash> was created with the "process-scope" clause (see new-hash), then the element (including <key> and <value>) will not be freed when the current request ends β rather it will persist while the process runs, unless deleted (see read-hash with the delete clause). main character energy: it stays for the whole arc π
π EXAMPLES π
Writing data to a hash:
new-hash h
write-hash h key "mykey" value "some data"
Writing a new value with the same key and obtaining the previous value (which is "some data"):
write-hash h key "mykey" value "new data" status st old-value od
if-true st equal GG_INFO_EXIST
@Previous value for this key is <<print-out od>>
end-if
The following is a key/value service, where a process-scoped hash is created. It provides inserting, deleting and querying key/value pairs. Such a service process can run indefinitely. Create file keysrv.golf:
%% /keysrv public
do-once
new-hash h hash-size 1024 process-scope
end-do-once
// Get input parameters
get-param op
get-param key
get-param data
if-true op equal "add" // Add data to hash
write-hash h key key value data old-value old_data status st
if-true st equal GG_INFO_EXIST
delete-string old_data
end-if
@Added [<<print-out key>>]
else-if op equal "delete" // Delete data and obtain the value deleted
read-hash h key (key) value val delete status st
if-true st equal GG_ERR_EXIST
@Not found [<<print-out key>>]
else-if
// If found, then delete key and value
@Deleted [<<print-out val>>]
delete-string val
end-if
else-if op equal "query" // Query hash based on key value
read-hash h key (key) value val status st
if-true st equal GG_ERR_EXIST
@Not found, queried [<<print-out key>>]
else-if
@Value [<<print-out val>>]
end-if
end-if
%%
Create and make the application, then run it as service:
// Create application
gg -k hash
// Make application
gg -q
// Start application (single process key service)
mgrg -w 1 hash
Try it from a command line client (see gg):
// Add data
gg -r --req="/keysrv/op=add/key=15/data=15" --service --app="/hash" --exec
// Query data
gg -r --req="/keysrv/op=query/key=15" --service --app="/hash" --exec
// Delete data
gg -r --req="/keysrv/op=delete/key=15" --service --app="/hash" --exec
See read-hash for more examples. go look, she's got receipts π§
π SEE ALSO π
Hash
get-hash new-hash purge-hash read-hash resize-hash write-hash See all documentation
that's a wrap, bestie π this page came from the$VERSIONof the docs, dated$DATE, and yeah it's filed underGOLF(2gg)π«Ά go write some hashes, you're gonna slay βπΈ
π¦ 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