Posts

Go Language - Maps - Example

Image
A map is an unordered collection of key/value pairs, where each key is unique. We can create a new map with a make statement or a map literal. The default zero value of a map is nil. The len function returns the size of a map. Go Range keyword is used in different kinds of data structures in order to iterate over elements. Create a Map: Create a Map called users that will store int keys and String values: /* Create a new map * Empty map of string-string pairs */ users := make( map [ int ] string ) Add Items: Add a new key-value pair, //Add Items users[ 1 ] = "scott" users[ 2 ] = "june" users[ 3 ] = "april" users[ 4 ] = "carl" users[ 5 ] = "rick" users[ 6 ] = "daryl" Update Value: // Update value users[ 6 ] = "michonne" Iterate over all keys and values: // Iterating over all keys and values for id, name := range users { fmt.Printf( "id :%d name:

Go Arrays - Program to Find Largest & Smallest Element

Image
Example 1: Finding the largest element in an Array // Go program to find largest number in an Array package main import "fmt" // Main function func main() { // Let’s define an array first arr := [8] int { 2 , 11 , 22 , 5 , 6 , 3 , 1 , 9 } fmt.Println( "Largest Number:" , LargestNumber(arr)) //Another Example anotherarr := [8] int { 144 , 666 , 99 , 677 , 433 , 422 , 255 } fmt.Println( "Largest Number:" , LargestNumber(anotherarr)) } func LargestNumber(arr [8] int ) int { var temp int for i := 0 ; i < len(arr); i++ { for j := i + 1 ; j < len(arr); j++ { if arr[i] > arr[j] { temp = arr[i] arr[i] = arr[j] arr[j] = temp } } } return arr[len(arr)- 1 ] } Output: Example 2: Finding the smallest element in an Array // Go program to find smallest number in an Array package main import "fmt" // Main function f

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 := "