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
+10.9k Golang : Get month name from date example
+25.8k Golang : Record voice(audio) from microphone to .WAV file
+12.2k Golang : Get constant name from value
+11.6k Golang : Qt progress dialog example
+7.3k Golang : Count leading or ending zeros(any item of interest) example
+15.2k Golang : Set up source IP address before making HTTP request
+23.6k Golang : Storing cookies in http.CookieJar example
+11.2k Elastic Search : Return all records (higher than default 10)
+13.4k Golang : Execute function at intervals or after some delay
+5.4k Unix/Linux : Use netstat to find out IP addresses served by your website server
+7.5k Golang : Sort lines of text example
+13.4k Golang : Rename directory