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
+5.3k Android Studio : AlertDialog to get user attention example
+20.7k Golang : Print out struct values in string format
+19.7k Golang : Read directory content with filepath.Walk()
+6.5k Golang : Gaussian blur on image and camera video feed examples
+6.6k Golang : Build and compile multiple source files
+8.4k Golang : Create matrix with Gonum Matrix package example
+16.9k nginx: [emerg] unknown directive "passenger_enabled"
+17.2k Golang : Encrypt and decrypt data with TripleDES
+34.3k Golang : Convert(cast) int64 to string
+13.8k Golang : Capture stdout of a child process and act according to the result
+3k Golang : How to pass data between controllers with JSON Web Token