3 Easy Ways to Check If String is Empty in Golang

There are the following ways to check if a string is empty in Golang.

  1. Use the “comparison == operator”
  2. Use the “len()” function to check if the length of the string is equal to 0
  3. Use the “strings.TrimSpace()” function to check if the string only contains whitespace characters

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”. An empty string in Go is a string that has zero length and no characters. It can be represented by a pair of double quotes with nothing in between, such as “”.

Example

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.

Example

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.

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.

Example

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

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.”

Leave a Comment