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
+8.1k Golang : How to check if a string with spaces in between is numeric?
+14.9k Golang : Get IP addresses of a domain name
+10.5k Golang : Get remaining text such as id or filename after last segment in URL path
+21.2k Generate checksum for a file in Go
+17.8k Golang : Fix cannot download, $GOPATH not set error
+16.7k Golang : Get command line arguments
+3.5k Javascript : Detect when console is activated and do something about it
+25.5k Golang : dial tcp: too many colons in address
+5.5k Nginx : Password protect a directory/folder
+12.9k Golang : Check if a file exist or not
+14k Golang : Force download file example