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
+16.2k Golang :Trim white spaces from a string
+29.2k Golang : JQuery AJAX post data to server and send data back to client example
+7.1k Golang : Null and nil value
+4.8k HTTP common errors and their meaning explained
+8.8k Golang : Sort lines of text example
+10.4k Android Studio : Simple input textbox and intercept key example
+23.3k Golang : Get ASCII code from a key press(cross-platform) example
+19.7k Golang : Append content to a file
+9.6k Random number generation with crypto/rand in Go
+13.6k Golang : Tutorial on loading GOB and PEM files
+8.4k Golang : Add text to image and get OpenCV's X, Y co-ordinates example
+10.1k Golang : How to check if a website is served via HTTPS