Example of auto status checking with Golf
When talking about safety in programming, it is memory safety that usually takes the spotlight. However, here we'll touch on another common safety issue that typically causes application logic errors, meaning your program won't function correctly. It's about checking the status of any kind of statement that provides it. Not checking the status could mean that the result of a statement isn't correct, yet your program is proceeding as if it were. Of course, this is bound to cause problems, and this is true for any programming language.
Golf now has a built-in mechanism to help prevent issues like this. The approach taken (at this time at least) is not to force the developer to check status for every statement. Rather if status isn't checked, your program will stop in an orderly fashion and tell you exactly in which source file and which line number you didn't check the status that may cause an issue. It's a default fine line between code bloat and safety. Not every status needs to be checked. And this approach helps pinpoint the most pressing spots in your code where failure is the most likely to happen.
Another future enhancement is in the queue, which would allow (as an option) to force status checking on all statements.
Create an application in a separate directory:
mkdir status-check
cd status-check
gg -k file-transform
Copied!
Put the following code in file "file-name.golf":
%% /file-name public
silent-header
get-param fname
change-dir run-dir
read-file fname to file_content
match-regex "File: ([0-9]+).imp" in file_content replace-with "\\1" result res cache
print-out res
%%
Copied!
This will take file name ("fname") as an input parameter, read that file, and transform any text in it that starts with "File: " followed by a file name that's made up of digits (without an extension), and outputs those file names. So a bit contrived example, and arguably the point here has nothing to do with matching regex patterns. But it's an example from a real-world application, so I plugged it in just the same.
So, let's say the input file is "myfile" with contents:
File: 99281.imp
File: 3716243.imp
File: 124.imp
Copied!
Make the application:
gg -q
Copied!
Run it:
gg -r --req="/file-name/fname=myfile" --exec
Copied!
And the output is, as it should be:
99281
3716243
124
Copied!
Okay, so far so good. But let's run this program by specifying a non-existent file:
gg -r --req="/file-name/fname=nonexistant" --exec
Copied!
The output is (among other things like stack trace):
File [file-name.golf], line [5], Statement failed without status being obtained and/or checked [-1]
Copied!
Aha! So your program will safely stop as soon there's a failure in code where you didn't check for status, namely this line (number 5) from the code above:
read-file fname to file_content
Copied!
Now, let's add status and check it, so replace "file-name.golf" with this:
%% /file-name public
silent-header
get-param fname
change-dir run-dir
read-file fname to file_content status st
if-true st not-equal GG_OKAY
@Sorry, cannot open file <<print-out fname>>!
end-if
match-regex "File: ([0-9]+).imp" in file_content replace-with "\\1" result res cache
print-out res
%%
Copied!
Compile it and run again:
gg -q
gg -r --req="/file-name/fname=nonexistant" --exec
Copied!
And the result is:
Sorry, cannot open file noexistant!
Copied!
So now you can see how useful the automatic "static checking" feature is. It prevented unsafe execution (logic-wise) from moving forward, and provided you with a clear clue as to how to fix the issue.
Articles
article-capi
article-cookies
article-debug
article-distributed
article-encryption
article-fetch-web-page
article-fifo
article-file-manager
article-hello-server
article-hello-world
article-hello-world-service
article-hello-world-service-web
article-how-to-create-golf-application
article-json
article-language
article-mariadb
article-memory-safety
article-memory-safety-web
article-notes-postgres
article-random
article-regex
article-remote-call
article-request-function
article-security
article-sendmail
article-server
article-shopping
article-sqlite
article-statements
article-status-check
article-tree
article-tree-web
article-vim-coloring
article-web-framework-for-c-programming-language
article-what-is-golf
article-what-is-web-service
See all
documentation
Copyright (c) 2019-2025 Gliim LLC. All contents on this web site is "AS IS" without warranties or guarantees of any kind.