3 Easy Ways to Check If String is Empty in Golang

An empty string in Go is a string that has zero length and no characters.

An empty string is represented by a pair of double quotes with nothing in between, such as “”.

This post will show various ways to check if a string is empty.

How to check if a string is empty in Golang

3 ways to check if a string is empty in Golang.

  1. Comparing an input string with an empty string using the == operator: if main_string == “”.
  2. Use the len() function to check if the length of the string is equal to 0: if len(main_string) == 0.
  3. Use the strings.TrimSpace() function to check if the string only contains whitespace characters: if strings.TrimSpace(main_string) == “”.

Method 1: Comparing an input string with an empty string

The best way to check if the string is empty is by comparing an input string with an empty string.

package main

import (
  "fmt"
)

func main() {
  stringFirst := ""
  stringSecond := "LastOfUs"

  if stringFirst == "" {
    fmt.Println("String First is empty")
  }

  if stringSecond == "" {
    fmt.Println("String Second is empty")
  }
}

Output

String First is empty

The above program declares two variables: stringFirst and stringSecond.

The first variable is initialized as an empty string, and the second is initialized with the string “LastOfUs”.

Using an if statement, the program then checks if each variable is an empty string. If the condition is true, it prints a message indicating that the corresponding variable is empty.

In our case, the program returns “String First is empty” but does not output anything for the second string.

Method 2: Considering the length of a string

Use the len() function to check if the length of the string is 0 and if it does; the string is empty otherwise, it does not.

package main

import (
  "fmt"
)

func main() {
  stringFirst := ""
  stringSecond := "LastOfUs"

  if len(stringFirst) == 0 {
    fmt.Println("String First is empty")
  }

  if len(stringSecond) == 0 {
    fmt.Println("String Second is empty")
  }
}

Output

String First is empty

The above program then checks if the length of each variable is equal to 0 using an if statement and the built-in len() function. If the condition is true, it prints a message indicating that the corresponding variable is empty.

In our case, the program will output “String First is empty” but not output anything for the second string.

This approach is more correct than the previous one as it checks whether the string has any characters.

Method 3: Using the strings.TrimSpace() function

The strings.TrimSpace() is a built-in function in Golang that removes any leading and trailing whitespace characters from a string.

If the strings.TrimSpace(main_string) function returns an empty string, which means that the original string only contained whitespace characters.

package main

import (
  "fmt"
  "strings"
)

func main() {
  stringFirst := " \n\t"
  if strings.TrimSpace(stringFirst) == "" {
    fmt.Println("The string only contains whitespace characters and it is empty")
  } 
  else {
    fmt.Println("The string contains non-whitespace characters.")
  }
}

Output

The string only contains whitespace characters and it is empty

The above program declares a variable stringFirst initialized with a string containing only whitespace characters.

Then it uses the strings.TrimSpace(stringFirst) function to remove any leading and trailing whitespace characters from the stringFirst.

It then compares the result of the strings.TrimSpace(stringFirst) function to an empty string using the == operator.

If the result is an empty string, the original string only contained whitespace characters, and the program will print “The string only contains whitespace characters and it is empty.” Otherwise, the program will print, “The string contains non-whitespace characters.”

Conclusion

The fastest and most reliable way to check if a string is empty in Golang is to check its length using the len() function, and if it is 0, that means the string is empty. The syntax is: if len(main_string) == 0.

Leave a Comment