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 runwhile this page was revised.
Quick answer: use s == "" for a truly empty string
package main
import "fmt"
func main() {
s := ""
if s == "" {
fmt.Println("empty")
}
}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.
package main
import "fmt"
func main() {
var s string
fmt.Println(s == "", s != "")
s = "go"
fmt.Println(s == "", s != "")
}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.
package main
import "fmt"
func main() {
s := ""
fmt.Println(len(s) == 0, len(s) > 0)
}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
package main
import (
"fmt"
"strings"
)
func main() {
s := " \t "
fmt.Println("raw empty?", s == "")
fmt.Println("trim empty?", strings.TrimSpace(s) == "")
}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
package main
import (
"fmt"
"strings"
)
func main() {
name := " Ada "
if strings.TrimSpace(name) != "" {
fmt.Println("has content after trim")
}
}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):
var s string
_ = s == nil // invalid: string cannot be compared to nilRight:
package main
import "fmt"
func main() {
var s string
fmt.Println(s == "")
}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
*stringor 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.

