Golang Program to find the length of a string


What is string in golang?

In Go, a string is a sequence of characters. It is an immutable data type, meaning that once a string is created, it cannot be modified. Strings are enclosed in double quotes ("") and can contain any combination of letters, numbers, and symbols. They are commonly used to store text and are often used as input and output in programs.

Method 1: By Using An External Function

In this method, the function that we create will accept the string whose length is to be calculated as an argument and returns the final length of the function in integer format.

Algorithm

  • Step 1 − First, we need to import the fmt package.

  • Step 2 − Then, we need to create a function named stringLength().

  • Step 3 − Create a for loop to iterate over the characters of the string and in every iteration it is incrementing the length variable.

  • Step 4 − Now, start the main() function.

  • Step 5 − Inside the main() initialize a variable of string data type and print it on the screen.

  • Step 6 − Now, call the stringLength() function by passing the string as an argument and store the result in a new variable.

  • Step 7 − Print this variable on the screen using fmt.Println() function.

Example

In this example, we will use an external user-defined function to find the length of a string by using an external function.

package main
import "fmt"
// Function to find the length of a string
func stringLength(str string) int {
   var length int
   for range str {
      length++
   }
   return length
}
func main() {
   // Initializing a string
   str := "Hello, world!"
   fmt.Println("The given string is:", str)
   var result int = stringLength(str)
   fmt.Println("Length of the above String is:", result)
}

Output

The given string is: Hello, world!
Length of the above String is: 13

Method 2: Using Internal Package

In this method, we will write a go language program to find the length of a string by using internal package.

Syntax

func RuneCountInString(s string) (n int)

The RuneCountInString() function is present in utf-8 package and is used to find the length of a string. This function accepts the string whose length is to be calculated as argument to a function and returns the length of it in integer.

func TrimSpace(str string) string

The trimSpace() function is present in strings package and is used to remove the spaces between the characters of a string. The function accept the required string as an argument and returns the final string obtained after removing the spaces between the words of it.

func len(v Type) int

The len() function is used to get the length of a any parameter. It takes one parameter as the data type variable whose length we wish to find and returns the integer value which is the length of the variable.

func Count(s, sep []byte) int

The Count() function is present in bytes package and is used to calculate number of instances of a given slice. The function accepts two arguments one is the slice whose characters are to be counted and other is the separator whose occurrence is to be counted. The function then returns the integer value after counting the required characters.

Algorithm

  • Step 1 − First, we need to import the fmt and strings package.

  • Step 2 − Then, start the main() function.

  • Step 3 − Inside the main() function initialize a string type variable and store value to it.

  • Step 4 − Further, print the value on the screen using fmt.Println() function.

  • Step 5 − Now, use an internal function called TrimSpace() and pass the string as an argument to it.

  • Step 6 − Now, use the len() function to get the length of the resultant

  • Step 7 − print it on the screen by using fmt.Println() function.

Example 1

Go language example to find the length of a string by using strings package

package main
import (
   "fmt"
   "strings"
)
func main() {
   // initializing a string
   str := "Prevention is better than cure"
   fmt.Println("The given string is:", str)
   var result int = len(strings.TrimSpace(str))
   fmt.Println("Length of String:", result)
}

Output

The given string is: Prevention is better than cure
Length of String: 30

Example 2

In this example, we will use a go language program to find the length of a string by using the utf package.

package main
import (
   "fmt"
   "unicode/utf8"
)
func main() {
   // Initializing string
   str := "The way to get started is to quit talking and begin doing"
   fmt.Println("The given string is:", str)
   var result int = utf8.RuneCountInString(str)
   fmt.Println("Length of String:", result)
}

Output

The given string is: The way to get started is to quit talking and begin doing
Length of String: 57

Example 3

In this example, we have used bytes package in order to write a go language program to find the length of a string.

package main
import (
	"bytes"
	"fmt"
)
func main() {
   // Initializing string
   str := "The greatest glory in living lies not in never falling, but in rising every time we fall"
   fmt.Println("The given string is:", str)
   var result int = bytes.Count([]byte(str), nil)
   fmt.Println("Length of String:", result)
}

Output

The given string is: The greatest glory in living lies not in never falling, but in rising every time we fall
Length of String: 89

Conclusion

We have successfully compiled and executed a go language program to find the length of a string along with examples.

Updated on: 13-Feb-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements