Golang Program to check the given string is empty or not


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.

Syntax

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 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 accepts the required string as an argument and returns the final string obtained after removing the spaces between the words of it.

Algorithm

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

  • Step 2 − Now, start a function called isEmpty().

  • Step 3 − Use a for loop to iterate over the string.

  • Step 4 − Now, start the main() function. Inside the main() initialize a string variable and assign value to it.

  • Step 5 − Call the isEmpty() function by passing the string variable as argument to the function.

  • Step 6 − Use an if condition to check whether the value returned by the function is true or false.

  • Step 7 − Print that the string is empty if the value returned by the function is true otherwise print the string that is present in the variable initialized.

Example 1

In this example we will use a for loop to check if the given string is empty or not by using a for loop in an external function. The function we create will accept the string to be checked as argument and will return a Boolean variable based on whether the string has characters in it or not.

package main
import "fmt"
func isEmpty(str string) bool {
   var empty bool = true
   for i := 0; i < len(str); i++ {
      if str[i] != ' ' {
         empty = false
         break
		}
	}
   return empty
}
func main() {
	var input string
	input = "Easier way to succeed is by trying"
	fmt.Println("Getting first string")
	if isEmpty(input) {
		fmt.Println("The string is empty.")
	} else {
		fmt.Println("The given string is:\n", input)
	}
	fmt.Println()
	fmt.Println("Getting second string")
	input = ""
	if isEmpty(input) {
		fmt.Println("The string is empty.")
	} else {
		fmt.Println("The given string is:\n", input)
	}
}

Output

Getting first string
The given string is:
 Easier way to succeed is by trying

Getting second string
The string is empty.

Example 2

In this example we will use a inbuilt function len() in go language to find whether the given string is empty or not.

package main
import "fmt"
func main() {
	var input string
	fmt.Println("Example 1:")
	input = "This is a string"
	var result int = len(input)
	if result == 0 {
		fmt.Println("The string is empty.")
	} else {
		fmt.Println("The given string is:\n", input)
	}
	fmt.Println()
	fmt.Println("Example 2:")
	result = 0
	input = ""
	if result == 0 {
		fmt.Println("The given string is empty.")
	} else {
		fmt.Println("The given string is:\n", input)
	}
}

Output

Example 1:
The given string is:
 This is a string

Example 2:
The given string is empty.

Example 3

In this example we will use the TrimSpace() function present in strings package in order to check if the given string is empty or not.

package main
import (
	"fmt"
	"strings"
)
func main() {
	var input, result string
	result = ""
	input = "Try and try until you get the success"
	fmt.Println("Example 1:")
	if result == strings.TrimSpace(input) {
		fmt.Println("The given string is empty")
	} else {
		fmt.Println("The given string is:\n", input)
	}
	fmt.Println()
	fmt.Println("Example 2:")
	input = ""
	if result == strings.TrimSpace(input) {
		fmt.Println("The given string is empty")
	} else {
		fmt.Println("The given string is:\n", input)
	}
}

Output

Example 1:
The given string is:
 Try and try until you get the success

Example 2:
The given string is empty

Example 4

In this example we will use the ValueOf() function present in reflect package in order to check if the given string is empty or not.

package main
import (
	"fmt"
	"reflect"
)
func main() {
	var input string
	input = "This is some string."
	fmt.Println("Example 1:")
	if reflect.ValueOf(input).String() == "" {
		fmt.Println("The given string is empty")
	} else {
		fmt.Println("The given string is:\n", input)
	}
	fmt.Println()
	fmt.Println("Example 2:")
	input = ""
	if reflect.ValueOf(input).String() == "" {

		fmt.Println("The given string is empty")
	} else {
		fmt.Println("The given string is:\n", input)
	}
}

Output

Example 1:
The given string is:
 This is some string.

Example 2:
The given string is empty

Conclusion

We have successfully compiled and executed a Golang Program to check the given string is empty or not along with examples. Here we have used both library functions and for loops in order to implement this result.

Updated on: 13-Feb-2023

640 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements