Let us take a look at the next function:
def getKey():
key = 0
while True:
print('Enter the key number (1-%s)' % (MAX_KEY_SIZE))
key = int(input())
if (key >= 1 and key <= MAX_KEY_SIZE):
return key
In this function Python uses the while True: construct. The input() function is used to capture the
key offset and is converted to an int. The next if statement does some validation to make sure the entered key is between a value of 1 and the Max_Key_Size.
Let us now look at the converted Go code:
// Get the key value for the cipher offset
func getKey() rune {
var key rune
for {
fmt.Printf("Enter the key number (1 - %d)", maxKeySize)
fmt.Scanln(&key)
if key >= 1 && key <= maxKeySize {
return key
}
}
}
The biggest noticeable change in this code snippet is the use of the rune datatype. In Go the Rune data type is an alias for int32. Rune data types are mapped to unicode values. So if you stored the character 'a' to a Rune data type, the value stored would be 97.
Go does not support a while construct, only the for construct so you can use the for construct without an iterator, conditional statement or increment statement, and you get the for True evaluation.
Since we are looking for a single int value we can use the fmt.Scanln() function. The if statement contains the same expression evaluation.
No comments:
Post a Comment