OneBite.Dev - Coding blog in a bite size

check if a string is empty in Go

Code snippet on how to check if a string is empty in Go

  package main

  import "fmt"

  func main() {
    testString := ""

    // Check if string is empty
    if testString == "" {
        fmt.Println("String is empty")
    } else {
        fmt.Println("String is not empty")
    }
  }

This code is written in Go and is used to check if a string is empty. The code begins by creating a string variable named testString and assigning it the empty string. On the next line, an if-else statement is used to compare the testString with the empty string. If they are the same, a “String is empty” message is printed. Otherwise, a “String is not empty” message is printed. The code ends with a closing bracket for the main function.

go