Posts

How to iterate over a map in Go?

Image
A map is an unordered collection of key/value pairs, where each key is unique. Go Range keyword is used in different kinds of data structures in order to iterate over elements. Example 1: Iterate over all keys and values // Go program to illustrate how // to iterate the map package main import "fmt" // Main function func main() { // Let’s define a map first usermap := map [ string ] string { "North America" : "Adam" , "Asia" : "Yusuke" , "Africa" : "Arif" , "South America" : "Rahul" , "Australia" : "Joy" , "Europe" : "Roman" , "Antarctica" : "Penguin" , } // Iterating over all keys and values for id, name := range usermap { fmt.Printf( "id :%s name: %s\n" , id, name) } } Output: Example 2: Iterate over all k

Hashing in Go Language - MD5 ,SHA-1, SHA-256, & SHA-512

Image
What does Hashing mean? A secure password hash is an encrypted sequence of characters obtained after applying certain algorithms and manipulations on user-provided password, which are generally very weak and easy to guess. Please remember that once this password hash is generated and stored in the database, you can not convert it back to the original password. MD5 hash in Go The MD5 message-digest algorithm is a widely used hash function producing a 128-bit hash value. Although MD5 was initially designed to be used as a cryptographic hash function, it has been found to suffer from extensive vulnerabilities. It can still be used as a checksum to verify data integrity, but only against unintentional corruption. It remains suitable for other non-cryptographic purposes, for example for determining the partition for a particular key in a partitioned database. Example: Go-MD5 to hash a String package main //Required imports import ( "crypto/md5" "encoding/hex"

Go Language - RSA Encryption And Decryption PKCS Example

Image
The RSA algorithm (Rivest-Shamir-Adleman) is a cryptographic algorithm that is used for specific security services or purposes, which enables public-key encryption and is widely used to secure sensitive data, particularly when it is being sent over an insecure network such as the HTTP. A public key is shared publicly, while a private key is secret and must not be shared with anyone. The following illustration highlights how asymmetric cryptography works: RSA Encryption and Decryption with PKCS1v15 Example, package main //Required imports for Encryption & Decryption import ( "crypto/rand" "crypto/rsa" "encoding/base64" "fmt" ) func main() { //2048 is the number of bits for RSA bitSize := 2048 //Generate RSA keys privateKey, err := rsa. GenerateKey(rand.Reader, bitSize) if err != nil { panic(err) } publicKey := privateKey.PublicKey //Your secret text secretMessage := "

Go - Palindrome Program Example

Image
If a number/string, which when read in both forward and backward way is the same, then such a number is called a palindrome number. Example 1. Go Program to check whether the given number is Palindrome or not //Check Whether a Number is Palindrome or Not package main import "fmt" // Main function func main() { number1 := 121 number2 := 125 number3 := 125521 IsPalindrome(number1) IsPalindrome(number2) IsPalindrome(number3) } func IsPalindrome(number int ) { var lastDigit int sum := 0 a := number // Code to reverse a number for a != 0 { lastDigit = a % 10 //getting remainder sum = (sum * 10 ) + lastDigit a = a / 10 } /*If given number equal to sum than number is palindrome otherwise not palindrome*/ if sum == number { fmt.Println(number, " is palindrome " ) } else { fmt.Println(number, " is not palindrome" ) } } Output: Example 2. Go Progra

Go Arrays - Program to find the duplicate elements of an Array

Image
Go program to find the duplicate numbers in an Array // Go program to find duplicate numbers in an Array package main import "fmt" // Main function func main() { // Let’s define an array first arr := [10] int { 33 , 2 , 1 , 22 , 2 , 6 , 3 , 1 , 33 , 9 } for i := 0 ; i < len(arr); i++ { for j := i + 1 ; j < len(arr); j++ { if arr[i] == arr[j] { fmt.Println(arr[j]) } } } } Output: Go program to find the duplicate words in an Array // Go program to find duplicate words in an Array package main import "fmt" // Main function func main() { // Let’s define an array first arr := [ 10 ] string { "Go" , "Java" , "Javascript" , "Python" , "Php" , "Kotlin" , "Vue" , "C" , "Go" , "Python" , } for i := 0 ; i < len(arr); i++ { for j := i + 1 ; j