Go(lang) keywords
“ Keywords are predefined, reserved words used in programming that have special meanings to the compiler.” They’re essentially identifiers reserved for the compiler, that provide some kind of functionality. For example, keyword while
will tell the compiler to initiate a loop. This is a valid statement for most, if not all, programming languages. Go offers some not-so-common keywords. In this post, I’ll highlight some of Go’s rare keywords.
Goto statement
“Goto statement allows unconditional jump to a labeled statement with in the same function.” It allows programmers to specify where the compiler should start executing at next. One use case of goto is within the Go math
package (https://go.dev/src/math/gamma.go). Goto is a relic of the past, with it being labelled as buggy and bad for readability. It is not a recommended practice, but since we take risks on this blog, I’ll demonstrate a use case of the goto keyword.
Let’s say I have a function that will write to the console based on a supplied number parameter. Regardless of the parameter value, an output will be made. The function should also perform one operation on the value before writing output. The function will look like this :
I can rewrite this function to use keyword goto. Since the function will write to output regardless of a
’s value, I’ll move it into label OUTPUT. I’ll proceed by adding statement goto OUTPUT
after multiplying variables a and b. The program will then skip evaluating the remaining if
statements and write the output. The code will look like this :
There are other ways to resolve this problem without keyword goto
. However, these methods, in my opinion, would require more code. If I opted to write an abstract method for OUTPUT, I would need a return statement after each condition is met, to limit the function to performing one operation on the value. Sometimes you may need multiple evaluations on a variable, this is where keyword fallthrough
shines.
Fallthrough keyword
“fallthrough keyword is used in switch statement in golang.” It enables transfer of control “to the next case even though the current case might have matched.” You may be wondering what is a good use case of this keyword?
The gist
Below is a basic example of keyword fallthrough
.
The code will output :
As observed with the 2 log entries, keyword fallthrough enables the execution of multiple cases. Without fallthrough
, only one output will be made, which will be i is less than 50
.
You may have heard of the coin change programming challenge. The challenge consists of writing a function that given an amount of money, will return the number of different denominations to make up the change. For example, if the given amount of money is $2.39, the array returned should be [2, 1, 1, 0, 4]. This array will denote (in order, from left to right): 2 dollars, 1 quarter, 1 dime and 4 pennies. I’m going to write a function utilizing the fallthrough keyword to solve the coin change challenge. The code will solve the challenge by using a brute force approach. Here is an implementation of it :
For this use case, fallthrough ensures each denomination is processed. For each case, the remaining number is divided by the denomination and rounded down to the nearest whole number. This number is assigned to its corresponding slot in the result array. The remaining number becomes the modulo (remainder) of the amount of change left and the denomination.
Here is the function implementation and output :
The first invocation of makeChange
will return 1 dollar, 1 quarter and 1 dime as the change to get 1.35. The second invocation will return 1 dollar, 1 quarter, 1 dime and 4 pennies as the change to get 1.39. There are other ways to reproduce this behavior without the fallthrough statement. One method could be by looping through an array of integers consisting of denomination values. This new method would be easier to maintain, as it’ll have fewer moving parts. I’m mentioning this to illustrate that usage of fallthrough
is not the only solution to this problem. Use the keyword at your own discretion.
Conclusion
Go has other keywords to offer beyond the ones mentioned. I covered goto
since it is considered bad practice, and fallthrough
, because I could use it to solve the coin change challenge. While using Go keywords, it’s important to verify a keyword’s impact on source code readability.