Posts

Showing posts with the label Go Language

How to check if a String is an Integer in Go Language

Image
Hello everyone, in this tutorial, we’ll explore multiple ways to detect if the given String is an int. Example 1: Using strconv.Atoi() function package main import ( "fmt" "strconv" ) func main() { str1 := "5674" str2 := "123o" fmt.Println(IsInt(str1)) fmt.Println(IsInt(str2)) } func IsInt(str string ) bool { _, err := strconv.Atoi(str) if err != nil { return false } else { return true } } Output: Author: Joy More Related Topics, How to make a delay in Go Language Program to check if a string contains the substring How to Split a String by Space in Go Language? Trigonometry with Go - Sin, Cos, Tan Functions Examples How to iterate over a map in Go? Go Arrays - Program to Find Largest & Smallest Element Go Arrays - Program to find the duplicate elements of an Array Go - Factorial Program using loop Go - Palindrome Program Example Go Program to print Odd and Even Numbers from an

How to find the sum of an Array/Slice of numbers in Go Language

Image
Given an  Array  and  Slice  of Integers, Write a Go Language Program to find the sum of the elements of the  Array  and Slice . Note: Once an  Array  has allocated its size, the size can no longer be transmuted.  A  Slice  is a variable-length version of an  Array , providing more flexibility for developers. A range clause provides a way to iterate over an  Array   or  Slice. Example 1: Program to find the sum of the elements of an Array using for loop range package main import ( "fmt" ) func Sum(numb [ 6 ] int ) int { output := 0 for _, n := range numb { output += n } return output } func main() { num := [ 6 ] int { 5 , 1 , 7 , 2 , 3 , 6 } fmt.Println( "Sum :" , Sum(num)) } Output: Example 2: Program to find the sum of the elements of a Slice using for loop range package main import ( "fmt" ) func Sum(numb [] int ) int { output := 0 for _, n := range numb { output += n } return

Go Language - Program to Reverse a String

Image
Example 1: Using the loop //Go Program to reverse a string using loop package main import "fmt" // Main function func main() { str1 := "Earth" str2 := "moon" fmt.Println(Reverse(str1)) fmt.Println(Reverse(str2)) } func Reverse(str string ) string { var reverse string // Code to reverse a string for i := len(str) - 1 ; i >= 0 ; i-- { reverse = reverse + string(str[i]) } return reverse } Output: Example 2: Using recursion //Go Program to reverse a string using recursion package main import ( "fmt" ) // Main function func main() { str1 := "Earth" str2 := "moon" fmt.Println(Reverse(str1)) fmt.Println(Reverse(str2)) } func Reverse(str string ) string { return reverseText(str, len(str)- 1 ) } // This is reversing the string elements recursively func reverseText(text string , location int ) string { // Condition to stop the recursion process

Go Language - Multilevel Data Encryption Using AES and RSA - Implementation

Image
Advanced Encryption Standard(AES) is a symmetric encryption algorithm. AES encryption is used by the U.S. for securing sensitive but unclassified material, so we can say it is enough secure. It allows 128 bit, 192 bit and 256-bit encryption. Symmetric encryption is very fast as compared to asymmetric encryption and are used in systems such as database system.  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. Encryption's primary purpose is to protect against brute force attacks. It is composed of a cipher, the message/data, and a key (the password). With a wide range of free tools available like(Aircrack-ng, John the Ripper, Rainbow Crack, L0phtCrac

Go Language - Program to Check Whether a Number is Prime or Not

Image
Prime numbers are natural numbers that are only divisible by 1 and the number itself.  A prime number cannot be divided by any other numbers without leaving a remainder.  An example of a prime number is 17. It can only be divided by 1 and 17, which has only two factors 1 and 5. Let's take another example of the number 6, which has more than two factors, i.e 1, 2, 3, and 6. This means 6 is not a prime number. Example 1. Go Program to check whether the given number is Prime or not package main import ( "fmt" ) func main() { CheckPrimeNumber( 11 ) CheckPrimeNumber( 1 ) CheckPrimeNumber( 23 ) CheckPrimeNumber( 20 ) CheckPrimeNumber(- 9 ) } func CheckPrimeNumber(number int ) { isPrime := true if number < 0 { fmt.Println( "Number must be greater than 0" ) } else if number == 0 || number == 1 { fmt.Println(number, "is not a prime number" ) } else { for i := 2 ; i <= number/ 2 ;