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
+23.8k Golang : Fix type interface{} has no field or no methods and type assertions example
+27.5k Golang : dial tcp: too many colons in address
+25.3k Golang : Convert long hexadecimal with strconv.ParseUint example
+5.3k Javascript : Shuffle or randomize array example
+6.2k Golang : Calculate US Dollar Index (DXY)
+33.8k Golang : convert(cast) bytes to string
+5.7k Golang : Struct field tags and what is their purpose?
+9.4k Golang : Detect Pascal, Kebab, Screaming Snake and Camel cases
+12.3k Golang : Validate email address
+12.2k Golang : Simple client-server HMAC authentication without SSL example
+15.6k Chrome : ERR_INSECURE_RESPONSE and allow Chrome browser to load insecure content
+8.2k Golang : Qt splash screen with delay example