How to check if a string is empty in Go

If you need to check if a string is empty in Go, then there are two possible ways to immediately verify this:

Option 1 – Compare the length:

if len(s) > 0 { // do something with string }
Code language: Go (go)

Option 2 – Compare against blank:

if s != "" { // do something with string }
Code language: Go (go)
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments