TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to GO

Golang Caesar Cipher Method

Use a Caesar cipher on text to shift characters. Invoke the strings.Map method.
Caesar cipher. The Roman army attacks at once. This command was delivered as a cipher, in a Caesar cipher. The code "exxegoexsrgi" is a cipher code.Strings
By shifting letters, we can encode a message. This deters casual snooping. In Go we can implement this with the strings.Map method.
Method. Let us begin. We introduce the caesar() method. This receives a rune and returns a modified rune. It shifts characters, and then shifts characters to a valid range.

Main: In this method control flow begins. To call caesar() within the strings.Map method, we use funcs.

Func: Each func we define calls the caesar() method with a special shift argument. This shifts characters by that number of places.

Note: The strings.Map method can only call a method that receives and returns a rune. The shift argument is specified in a wrapper func.

Warning: A global variable could be used to control the shift number, but this is not accepted as good programming practice.

Golang program that implements caesar cipher package main import ( "fmt" "strings" ) func caesar(r rune, shift int) rune { // Shift character by specified number of places. // ... If beyond range, shift backward or forward. s := int(r) + shift if s > 'z' { return rune(s - 26) } else if s < 'a' { return rune(s + 26) } return rune(s) } func main() { value := "test" fmt.Println(value) // Test the caesar method in a func argument to strings.Map. value2 := strings.Map(func(r rune) rune { return caesar(r, 18) }, value) value3 := strings.Map(func(r rune) rune { return caesar(r, -18) }, value2) fmt.Println(value2, value3) value4 := strings.Map(func(r rune) rune { return caesar(r, 1) }, value) value5 := strings.Map(func(r rune) rune { return caesar(r, -1) }, value4) fmt.Println(value4, value5) value = "exxegoexsrgi" result := strings.Map(func(r rune) rune { return caesar(r, -4) }, value) fmt.Println(value, result) } Output test lwkl test uftu test exxegoexsrgi attackatonce
Some notes. The strings.Map is a good way to translate characters in strings. But its first argument is a func that only receives a rune (and no shift value).

So: We can modify how a method is called by adding another "wrapper" method that specifies the shift in an internal method calls.

Note: In some ways, this implementation is less clear than a custom looping method, but using strings.Map in this way is reliable.

Uppercase letters. This method will not work correctly on uppercase letters. To add support for uppercase letters, please try adding another pair of if-else statements.If
A summary. The Caesar cipher is similar to the ROT13 cipher, but it accommodates any shift value. It is not useful for battles anymore. But it can help us learn more about how to use Go.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf