How to check empty string in golang?

Hi Friends 👋,

Welcome To aGuideHub! ❤️

Today, we are going to learn How to check empty string in golang?, here we will use some common syntax to validate string empty or not.

Here, we will take two variables first one is empty and second is not, then we will put same condition on both after that we will see the output.

To check empty string variable, we will compare with "" empty string, if both are equal then it’s empty and if not then it’s not empty.

let’s write a code.

package main

import (

func main() {
  // initialize an empty and a non-empty string
  str1 := ""
  str2 := "edpresso"

  if str1 == "" {
    fmt.Println("String 1 is empty")
  } else {
    fmt.Println("String 1 is not empty")

  if str2 == "" {
    fmt.Println("String 2 is empty")
  } else {
    fmt.Println("String 2 is not empty")

As per expectation, it should print that the first one is empty, and the second one is not empty.

let’s check the output.

check, empty string,golang

Try it yourself

All the best 👍

Premium Content

You can get all the below premium content directly in your mail when you subscribe us


Interview Questions

Soon You will get CSS, JavaScript, React Js, and TypeScript So Subscribe to it.

Portfolio Template

View | Get Source Code

Cheat Sheets

Cheat Sheets Books are basically Important useful notes which we use in our day-to-day life.

Related Posts