Skip to content

How to Check if a String Is Empty in Golang

This is the easiest thing I want to mention! Just use the equal (==) operator in golang:

// To check if it is empty
myString == ""

// To check if it is not empty
myString != ""

Code example:

package main

import "fmt"

func main() {
	var myName string

	if myName == "" {
		fmt.Println("The name variable is empty")
	}

	myName = "Morteza"

	if myName != "" {
		fmt.Println("The name variable is:", myName)
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *