Posts

Showing posts with the label Go

How to iterate over words of String in Go Language?

Hello everyone, in this tutorial we will show you how to iterate over all the words of String in the Go Programming language. How to iterate over all the words of the string in Go Language? Using for-range loop package main import ( "fmt" "strings" ) func main() { teststr := "This is a sample sentence." str := strings.Fields(teststr) for _, word := range str { fmt.Println(word) } } Output: This is a sample sentence. More Go language topics, How to make a delay in Go Language How to iterate over all the characters of the string in Go Language Trim a string in Go Language Check if a String is an Integer in Go Language Sort a String Array/Slice 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 t

Trim a string in Go Language - Examples

Image
Hello everyone, In this tutorial, we will show you how to trim all of the leading and trailing spaces in a string. Few methods from the strings package are, Trim():  TrimSpace() TrimLeft() TrimRight() TrimPrefix() TrimSuffix() Example 1:  strings.Trim() function The Trim() function is an inbuilt method of the strings package. This method  will remove the leading and trailing whitespace from a string that is received as a parameter. // Golang program to demonstrate the // example of strings.Trim() Method package main import ( "fmt" "strings" ) func main() { str1 := " My world is too small " fmt.Println( "Before:" , str1) str4 := strings.Trim(str1, " " ) fmt.Println( "After:" , str4) } Output: Example 2:  strings.TrimSpace() function The TrimSpace() function is an inbuilt method of the strings package. This is a better choice if a variety of whitespace runes may be present at the start or end of a

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