Golang Check If String Is Empty: Empty vs Blank String

Check if a string is empty in Go with s == "" or len(s) == 0, tell empty from whitespace-only strings, use strings.TrimSpace for blank input, and remember string is never nil.

Published

Updated

Read time 5 min read

Reviewed byDeepak Prasad

Golang Check If String Is Empty: Empty vs Blank String

You usually need one of two checks: whether a string has no characters at all, or whether it is “blank” for your app after stripping leading and trailing whitespace. The first is s == "". The second is strings.TrimSpace(s) == "". Mixing them up is the most common bug when validating forms, flags, or lines from a reader.

For general string comparison beyond emptiness, that article covers ordering and helpers.

Tested on: Go 1.22 on 64-bit Linux; snippets were run with go run while this page was revised.


Quick answer: use s == "" for a truly empty string

go
package main

import "fmt"

func main() {
	s := ""
	if s == "" {
		fmt.Println("empty")
	}
}
Output

That prints empty. If spaces, tabs, or newlines should count as empty too, trim first: strings.TrimSpace(s) == "".


Check If a String Is Empty in Go

Use s == "" for a simple empty string check

The zero value of string is "". A declared var s string is already empty until you assign something else.

go
package main

import "fmt"

func main() {
	var s string
	fmt.Println(s == "", s != "")

	s = "go"
	fmt.Println(s == "", s != "")
}
Output

The first line shows both comparisons for the zero value; the second line shows a non-empty value.

Use len(s) == 0 as an alternative

len(s) == 0 matches s == "" for normal strings: length counts UTF-8 bytes. Pick one style in a file; many teams prefer s == "" because it states the intent directly.

go
package main

import "fmt"

func main() {
	s := ""
	fmt.Println(len(s) == 0, len(s) > 0)
}
Output

Empty String vs Blank String in Go

An empty string is "": length zero, no runes. A string like " " or "\t\n" is not empty—it contains space characters. Your UI may still call that “blank”; then trim before you compare.

Why " " is not an empty string

Use strings.TrimSpace to check blank strings

go
package main

import (
	"fmt"
	"strings"
)

func main() {
	s := "   \t  "
	fmt.Println("raw empty?", s == "")
	fmt.Println("trim empty?", strings.TrimSpace(s) == "")
}
Output

The first question is false; the second is true for this value.


Check If a String Is Not Empty

Use s != "" for non-empty strings

Use strings.TrimSpace when spaces should not count

go
package main

import (
	"fmt"
	"strings"
)

func main() {
	name := "  Ada  "
	if strings.TrimSpace(name) != "" {
		fmt.Println("has content after trim")
	}
}
Output

TrimSpace returns a new string; it does not change name in place. Assign t := strings.TrimSpace(name) if you need the trimmed value later.


s == "" vs len(s) == 0

Which one is more readable?

s == "" reads as “this should be empty.” len(s) == 0 reads as “length math.” For a plain emptiness guard, prefer s == "".

Is there any performance difference?

For typical code paths the compiler treats both checks similarly. Choose clarity first; micro-benchmarks are not the reason to pick one.

Check Meaning
s == "" No bytes in the string
s != "" At least one byte
len(s) == 0 Same as s == "" for string
len(s) > 0 Same as s != "" for string
strings.TrimSpace(s) == "" No non-space content after Unicode trim

Can a Go string be nil?

No. A plain string is never nil; its zero value is "". Only pointers, slices, maps, channels, interfaces, and function values can be nil. If you need “missing” separate from “empty,” use *string and compare the pointer to nil, then dereference when it points at data.

Wrong (does not compile):

text
var s string
_ = s == nil // invalid: string cannot be compared to nil

Right:

go
package main

import "fmt"

func main() {
	var s string
	fmt.Println(s == "")
}
Output

Common use cases

Validate required fields with s == "" after you know the value. For environment variables or config strings, the same check tells you “unset or explicitly empty,” depending on how you load settings. Optional struct fields use "" as the zero value, so obj.Label == "" is a normal pattern.

Interactive input from bufio.Reader.ReadString('\n') or fmt.Scanln often keeps a newline; a user who only presses Enter may give you "\n", not "". Compare strings.TrimSpace(line) == "" when a blank line should count as empty. For fuller stdin patterns see parsing multiple inputs.


Common mistakes

Treating whitespace as empty

" " is not empty until you trim. Use strings.TrimSpace when your rules say whitespace-only is empty.

Checking nil on a string

Compare to "", not nil, for value strings.

Using len after trimming without storing the result

strings.TrimSpace(s) returns a new string. If you need the trimmed text for later logic, assign it once and reuse the variable.

Overcomplicating the check

For “is this string empty?” the idiomatic answer stays s == "". Wrap trim or length logic only when your product rules need it.


Go empty string cheat sheet

Goal Use
Truly empty s == ""
Not empty s != ""
Length zero len(s) == 0
Whitespace-only counts as empty strings.TrimSpace(s) == ""
Optional field empty obj.Field == ""
Trim once, then test t := strings.TrimSpace(s); t == ""

Which check should you use?

  • Only care about zero characters: s == "".
  • Care about “blank” user input: trim, then compare to "".
  • Need three states (missing, empty, text): consider *string or a dedicated type.

Summary

Use s == "" (or s != "") for the usual golang check if string is empty. len(s) == 0 is equivalent but less direct for readers. Treat spaces and newlines as non-empty unless you call strings.TrimSpace first. Remember strings are never nil; the zero value is "". Keep helpers small and match your validation rules instead of copying trim logic in many places.


References


Frequently Asked Questions

1. How do I check if a string is empty in Go?

Use s == "".

2. How do I check if a string is not empty in Go?

Use s != "".

3. Is len(s) == 0 valid?

Yes. It is equivalent to s == "" for UTF-8 byte length; s == "" is usually clearer when you only care about emptiness.

4. What is the zero value of a string in Go?

The zero value is the empty string "".

5. Can a Go string be nil?

No. string is a value type; compare with "". Use a pointer *string if you need nil for missing values.

6. How do I check if a string contains only spaces?

Use strings.TrimSpace(s) == "" after importing package strings.

7. Is " " an empty string?

No. It has length and contains runes; it is whitespace-only. Trim first if that should count as empty.

8. Why does my empty line test fail after ReadString?

ReadString often keeps the delimiter; a bare Enter yields "\n", which is not equal to "" until you TrimSpace or trim the newline yourself.

9. How does this relate to comparing strings?

Empty checks use == on strings; see comparing strings in Go for ordering, case, and prefix or suffix checks.
Azka Iftikhar

Computer Scientist

Proficient in multiple programming languages, including C++, Golang, and Java, she brings a versatile skillset to tackle a wide array of challenges. Experienced Computer Scientist and Web Developer, …