GOLF(2gg)
π GOLF(2gg) β write-string π
okay bestie, welcome to the 2gg manual, section Development. sit down, hydrate, and let's talk about building strings. π«Ά
β¨ NAME β¨
write-string β and it lives in the strings family π
πΈ PURPOSE πΈ
Create complex strings. that's it. that's the whole vibe. no cap. π
π¦ SYNTAX π¦
write-string <string>
<any code>
end-write-string [ notrim ]
π DESCRIPTION π
So here's the tea: output of any Golf code can be written into <string> with write-string. And in between write-string and end-write-string you can write <any Golf code>. fr fr β like, you can use database queries, conditional statements etc., just as you would for any other Golf code. it's giving β¨unlimited powerβ¨.
π« SHORTCUT CODE π«
not me about to save you keystrokes π₯° β instead of write-string you can also use a shortcut (( (and instead of end-write-string you can use ))). for example, here a string "fname" holds a full path of a file named "config-install.golf" under the application home directory (see directories):
get-app directory to home_dir
(( fname
@<<print-out home_dir>>/config-install.golf
))
lowkey iconic. π₯
π§ TRIMMING π§
Just like with all other Golf code, every line is trimmed both on left and write (yes bestie, on write, we're keeping it exactly as written π), so this:
(( mystr
@Some string
))
is the same as:
(( mystr
@Some string <whitespaces>
))
write-string (or (() statement must always be on a line by itself (and so does end-write-string, or )) statement). The string being built starts with the line following write-string, and ends with the line immediately prior to end-write-string.
All trailing empty lines are removed, for example:
(( mystr
@My string
@
@
))
the above string would have two trailing empty lines, however they will be removed. If you want to skip trimming the trailing whitespaces, use "notrim" clause in end-write-string.
β οΈ reminder bestie: "notrim" means you keep the trailing whitespace. that's a choice and we respect it. π«‘
π EXAMPLES π
π Simple
A simple example:
set-string my_str="world"
set-string my_str1="and have a nice day too!"
write-string result_str
@Hello <<print-out my_str>> (<<print-out my_str1>>)
end-write-string
print-out result_str
The output is
Hello world (and have a nice day too!)
ate. π
π Using code inside
Here is using Golf code inside write-string, including database query and conditional statements to produce different strings at run-time:
get-param selector
set-string my_str="world"
write-string result_str
if-true selector equal "simple"
@Hello <<print-out my_string>> (and have a nice day too!)
else-if selector equal "database"
run-query @db="select name from employee" output name
@Hello <<print-out name>>
@<br/>
end-query
else-if
@No message
end-if
end-write-string
print-out result_str
If selector variable is "simple", as in URL
https://mysite.com/<app name>/some-service?selector=simple
the result is
Hello world (and have a nice day too!)
If selector variable is "database", as in URL
https://mysite.com/<app name>/some-service?selector=database
the result may be (assuming "Linda" and "John" are the two employees selected):
Hello Linda
<br/>
Hello John
<br/>
If selector variable is anything else, as in URL
https://mysite.com/<app name>/some-service?selector=something_else
the result is
No message
three different vibes, one handler. we love a versatile queen. π
π Using call-handlers calls inside
The following uses a call-handler inside write-string:
set-string result_str=""
write-string result_str
@<<print-out "Result from other-service">> is <<call-handler "/other-service">>
end-write-string
print-out result_str
The "other-service" may be:
begin-handler /other-service public
@"Hello from other service"
end-handler
The output:
Result from other-service is Hello from other service
π¦ Nesting
An example to nest write-strings:
write-string str1
@Hi!
write-string str2
@Hi Again!
end-write-string
print-out str2
end-write-string
print-out str1
The result is
Hi!
Hi Again!
strings inside strings, it's giving inception but make it cute π§β¨
β SEE ALSO β
- Strings
concatenate-strings copy-string count-substring delete-string lower-string new-string read-split replace-string set-string split-string string-length trim-string upper-string write-string
See all documentation π
okay bestie that's the whole page!! no notes, 10/10, go build something gorgeous with it ππΈπ¦
this masterpiece of a page is officially brought to you by version $VERSION, dated $DATE, and it's the GOLF(2gg) manual, Development section. periodt. π
β¨
π¦ 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