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
+4.4k Golang : Return multiple values from function
+6.1k Golang : Trim everything onward after a word
+8k Golang : Detect Pascal, Kebab, Screaming Snake and Camel cases
+4.5k Golang : Shortening import identifier
+5.6k Golang : Totalize or add-up an array or slice example
+5.1k AWS S3 : Prevent Hotlinking policy
+8.2k Golang : How to get garbage collection data?
+5k Golang : Compound interest over time example
+9.2k Golang : How to profile or log time spend on execution?
+20.2k SSL : How to check if current certificate is sha1 or sha2
+10.5k Golang : Sort and reverse sort a slice of runes
+18.8k nginx: [emerg] unknown directive "passenger_enabled"