Golang : How to check for empty array string or string?
Sometimes that most obvious thing can be the hardest to remember. Putting this down as a note for myself and maybe useful to you too.
Problem :
Need to check if a string or []string is empty. How to do that?
Solutions :
if str is a string
len(strings.TrimSpace(str)) == 0
and let say str is an array []string
var str []string
strings.TrimSpace(strings.Join(str, "")) == ""
Reference :
https://groups.google.com/forum/#!topic/golang-nuts/7Ks1iq2s7FA
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+6.8k Golang : Identifying Golang HTTP client request
+14.8k Unmarshal/Load CSV record into struct in Go
+10.4k Golang : How to remove certain lines from a file
+9.6k Golang : rune literal not terminated error
+4.3k Get Facebook friends working in same company
+6.5k Golang : Launch Mac OS X Preview (or other OS) application from your program example
+4.6k Golang : Use modern ciphers only in secure connection
+7.8k Golang : How to tokenize source code with text/scanner package?
+7.6k Golang : Meaning of omitempty in struct's field tag
+11.9k Golang : Get IP addresses of a domain name
+3.2k PHP : Hide PHP version information from curl
+4.6k Golang : Totalize or add-up an array or slice example