skip to content
Alvin Lucillo

Idiomatic check for empty string

/ 1 min read

Both versions below achieve the same thing but version 1 is idiomatic in Go for the following reasons:

  1. It expresses intent, not mechanics — it’s easy to understand that you’re checking for empty value
  2. It is aligned wiht Go’s zero-value philosophy
  3. You know you’re dealing with string by looking at its comparison
// version 1
if str == "" {
	//...
}

// version 2
if len(str) == 0 {
	// ...
}