Header

Header

Tuesday, May 7, 2019

Learning Go Language

When I want to learn a new language I usually take an existing program and port that program to the new language I am learning.  I discovered a Python program out on the web that is simple and that I have ported before.

The program is a take on the Caesar Cipher encrypt and decrypt program.  Here is the link:

https://inventwithpython.com/chapter14.html

It turns out that this is a great program to convert to GO Language.  There is some character to ASCII conversion involved and GO Language deals with this conversion differently.

I will post the entire GO program later in this series on the GO Language.

Lets take a look at some of the issues I had to deal with:

 def getMode():  
   while True:  
     print('Do you wish to encrypt or decrypt a message?')  
     mode = input().lower()  
     if mode in 'encrypt e decrypt d brute b'.split():  
       return mode  
     else:  
       print('Enter either "encrypt" or "e" or "decrypt" or "d" or "brute" or "b".')  

Now let's take a look at the converted GO function:

 func getMode() string {  
      for {  
           fmt.Println("Do you wish to encrypt or decrypt or brute force a message?")  
           var mode string  
           fmt.Scanln(&mode)  
           mode = strings.ToLower(mode)  
           if strings.Contains("encrypt e decrypt d brute b", mode) {  
                return mode  
           }  
           fmt.Println("Enter either \"encrypt\" or \"e\" or \"decrypt\" or \"d\" or \"brute\" or \"b\".")  
      }  
 }  

Notice that in the Go Language example the fmt.Scanln() is used.  The Scanln() function is used, in this example, to capture a single alphanumeric character or word without white space.  The fmt package supports several difference functions to provide console input.  These functions can be combined with format verbs to control how the data in read from the console.  Please refer to the documentation on the fmt package for more details:  https://golang.org/pkg/fmt/
  • The obvious is the function declaration between Python 3 and GO Language
  • GO Language only supports a variety of for statements
  • GO Language supports printing and console input using the "fmt" package
  • GO Language supports double quotes and backticks
  • The "fmt" package supports a variety of print and scan functions
  • GO is stronger typed than Python

 fmt.Scanln(&mode)  

Notice the use of the ampersand character before the mode variable.  Yes Go supports pointers and the ampersand operator is referred to as the "Address of" operator.
Python 3 supports the following syntax:

 if mode in 'encrypt e decrypt d brute b'.split():   

GO does not support in .split() statement
I was able to perform the same operation by using the strings.Contains() function in the "strings" package.

 if strings.Contains("encrypt e decrypt d brute b", mode) {   

Python 3 did not require importing of any packages for the Caesar Cipher program.

The GO program required the following packages.

 import (  
      "bufio"  
      "fmt"  
      "os"  
      "strconv"  
      "strings"  
      "unicode"  
 )  

We will continue more conversion comparisons of the Caesar Cipher program in the next post.

No comments:

Post a Comment